我需要使用Node.js压缩整个目录.我目前正在使用node-zip,每次进程运行时都会生成一个无效的ZIP文件(从这个Github问题可以看出).
是否有另一个更好的Node.js选项可以让我压缩目录?
编辑:我最终使用归档
writeZip = function(dir,name) {
var zip = new JSZip(),
code = zip.folder(dir),
output = zip.generate(),
filename = ['jsd-',name,'.zip'].join('');
fs.writeFileSync(baseDir + filename, output);
console.log('creating ' + filename);
};
Run Code Online (Sandbox Code Playgroud)
参数的样本值:
dir = /tmp/jsd-<randomstring>/
name = <randomstring>
Run Code Online (Sandbox Code Playgroud)
更新:对于那些询问我使用的实现的人,这里是我的下载器的链接:
com*_*ted 102
我最终使用了归档库.效果很好.
var file_system = require('fs');
var archiver = require('archiver');
var output = file_system.createWriteStream('target.zip');
var archive = archiver('zip');
output.on('close', function () {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('error', function(err){
throw err;
});
archive.pipe(output);
archive.bulk([
{ expand: true, cwd: 'source', src: ['**'], dest: 'source'}
]);
archive.finalize();
Run Code Online (Sandbox Code Playgroud)
D.D*_*glo 26
我不假装展示新的东西,只是想为那些喜欢在他们的代码中使用Promise函数的人总结上面的解决方案(比如我).
const archiver = require('archiver');
/**
* @param {String} source
* @param {String} out
* @returns {Promise}
*/
function zipDirectory(source, out) {
const archive = archiver('zip', { zlib: { level: 9 }});
const stream = fs.createWriteStream(out);
return new Promise((resolve, reject) => {
archive
.directory(source, false)
.on('error', err => reject(err))
.pipe(stream)
;
stream.on('close', () => resolve());
archive.finalize();
});
}
Run Code Online (Sandbox Code Playgroud)
希望它会帮助某人;)
cai*_*ci2 11
Archive.bulk
现在已弃用,用于此的新方法是glob:
var fileName = 'zipOutput.zip'
var fileOutput = fs.createWriteStream(fileName);
fileOutput.on('close', function () {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.pipe(fileOutput);
archive.glob("../dist/**/*"); //some glob pattern here
archive.glob("../dist/.htaccess"); //another glob pattern
// add as many as you like
archive.on('error', function(err){
throw err;
});
archive.finalize();
Run Code Online (Sandbox Code Playgroud)
小智 10
要包含所有文件和目录:
archive.bulk([
{
expand: true,
cwd: "temp/freewheel-bvi-120",
src: ["**/*"],
dot: true
}
]);
Run Code Online (Sandbox Code Playgroud)
它使用下面的node-glob(https://github.com/isaacs/node-glob),因此任何与之兼容的匹配表达式都可以使用.
var zipper = require('zip-local');
zipper.sync.zip("./hello/world/").compress().save("pack.zip");
Run Code Online (Sandbox Code Playgroud)
child_process
api完成此操作。无需第三方库。两行代码。
const child_process = require("child_process");
child_process.execSync(`zip -r DESIRED_NAME_OF_ZIP_FILE_HERE *`, {
cwd: PATH_TO_FOLDER_YOU_WANT_ZIPPED_HERE
});
Run Code Online (Sandbox Code Playgroud)
我正在使用同步API。child_process.exec(path, options, callback)
如果需要异步,可以使用。除了指定CWD进一步细化您的请求外,还有更多选择。请参阅exec / execSync文档。
请注意: 此示例假定您在系统上安装了zip实用程序(至少OSX随附)。某些操作系统可能未安装实用程序(即,AWS Lambda运行时未安装)。在这种情况下,您可以在此处轻松获得zip实用程序二进制文件并将其与应用程序源代码打包在一起(对于AWS Lambda,您也可以将其打包在Lambda层中),或者您必须使用第三方模块(其中NPM有很多)。我更喜欢前一种方法,因为ZIP实用程序经过数十年的尝试和测试。