我需要使用 Node.js 压缩整个目录。我目前正在使用node-zip,每次该进程运行时都会生成一个无效的ZIP文件(正如您可以从这个 Github 问题 https://github.com/Stuk/jszip/issues/41).
是否有另一个更好的 Node.js 选项可以让我压缩目录?
编辑:我最终使用了archiver https://npmjs.org/package/archiver
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);
};
参数的样本值:
dir = /tmp/jsd-<randomstring>/
name = <randomstring>
UPDATE:对于那些询问我使用的实现的人,这是我的下载器的链接 https://github.com/commadelimited/JSDownloader/blob/master/routes/download.js:
我最终使用了archiver https://npmjs.org/package/archiver库。效果很好。
Example https://github.com/archiverjs/node-archiver/tree/master/examples
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);
// append files from a sub-directory, putting its contents at the root of archive
archive.directory(source_dir, false);
// append files from a sub-directory and naming it `new-subdir` within the archive
archive.directory('subdir/', 'new-subdir');
archive.finalize();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)