本文介绍了需要使用Node.js压缩整个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用Node.js压缩整个目录.我当前正在使用node-zip,每次运行该进程时,它都会生成一个无效的ZIP文件(如您从此Github问题).
I need to zip an entire directory using Node.js. I'm currently using node-zip and each time the process runs it generates an invalid ZIP file (as you can see from this Github issue).
还有另一个更好的Node.js选项可以让我压缩目录吗?
Is there another, better, Node.js option that will allow me to ZIP up a directory?
我最终使用了存档
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: For those asking about the implementation I used, here's a link to my downloader:
推荐答案
我最终使用了 archiver lib .效果很好.
I ended up using archiver lib. Works great.
Example
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();
这篇关于需要使用Node.js压缩整个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!