js-7z
Version:
🏭 7zip functions in NodeJS 🏭
80 lines (72 loc) • 2.02 kB
JavaScript
/**
* @author FellGill
* @license Apache 2.0
*
* Copyright 2020 FellGill
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const Arch = process.arch;
const ChildProcess = require('child_process');
function getSevenZPath() {
const SevenZPath = `7z/${Arch === 'x64' ? 'x64/7za.exe' : '7za.exe'}`;
if(__dirname.includes('node_modules')) {
return `./node_modules/js-7z/${SevenZPath}`;
} else {
return `./${SevenZPath}`;
}
}
/**
* Extract zip file
*
* @param {String} file - File that we want to decompress
* @param {String} out - Folder where we want the file to be decompressed
*/
exports.Extract = function(file, out, callback) {
var Process = ChildProcess.spawn(getSevenZPath(), ['x', `-o${out}`, file]);
Process.on('exit', function(exitCode) {
callback(exitCode);
});
};
/**
* Zip a folder
*
* @param {String} file - File URL
* @param {String} folder - Folder you want to compress
*/
exports.Compress = function(file, folder, callback) {
// Best compression method (?)
const CompressionArgs = [
'a',
'-t7z',
'-mx=9',
'-mfb=273',
'-ms',
'-md=31',
'-myx=9',
'-mtm=-',
'-mmt',
'-mmtf',
'-md=1536m',
'-mmf=bt3',
'-mmc=10000',
'-mpb=0',
'-mlc=0',
file,
folder,
];
var Process = ChildProcess.spawn(getSevenZPath(), CompressionArgs);
Process.on('exit', function(exitCode) {
callback(exitCode);
});
};