2013-03-26 70 views
45

到ZIP整个目录,我需要使用压缩的Node.js的整个目录我目前使用node-zip,并且每次进程运行时都会生成一个无效的ZIP文件(如您在this Github issue中看到的那样)。需要了解如何使用Node.js

是否有其他更好的,Node.js的选项,让我拉上了一个目录?

编辑:我结束了使用的参数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:对于那些询问我用的实现,here's a link to my downloader

+0

良好的通话@booyaa。谢谢。 – commadelimited 2013-03-26 16:09:54

+3

有人在Twitter上建议child_process API,并简单地调用系统ZIP:http://nodejs.org/api/child_process.html – commadelimited 2013-03-26 16:12:01

+1

我已经试过child_process方法。它有两个警告。 ** 1)** UNIX'zip'命令包括在压缩文件的当前工作目录的所有父文件夹的层次结构。这对你可能没问题,这不适合我。以某种方式改变child_process中的当前工作目录不会影响结果。 ** 2)**为了克服这个问题,你必须使用'pushd'跳进你将压缩的文件夹和'拉链-r',但因为'pushd'内置的bash,而不是/ bin/sh的你还需要使用/ bin/bash。在我的具体情况下,这是不可能的。只是一个头。 – johnozbay 2016-12-07 19:15:35

回答

71

最后我用archiver库。很棒。

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(); 
+1

似乎没有任何如何做到这一点的例子,你介意分享你做的事吗? – Sinetheta 2013-12-17 21:20:44

+3

当然可以。我会更新原始帖子。不幸的是, – commadelimited 2014-01-02 16:39:32

+1

归档器目前不支持文件名中的Unicode字符。报告给https://github.com/ctalkington/node-archiver/issues/90。 – Eye 2014-08-28 09:35:52

3

上将-zip的问题只是压缩现有的档案https://github.com/cthackers/adm-zip/issues/64以及cor破解压缩二进制文件。

我也跑进压缩腐败问题与节点拉链https://github.com/daraosn/node-zip/issues/4

节点存档器是似乎运作良好压缩只有一个,但它不具有任何解压缩功能。

+1

你在说什么节点归档? :github.com/archiverjs/node-archiver; github.com/richardbolt/node-archiver – biphobe 2016-02-24 08:22:56

+0

@firian他没有说Archiver,他说Adm-zip。 – 2016-03-16 12:19:41

+5

@FrancisPelland嗯,在他写的最后一句话中“_node-archiver是唯一一个似乎工作的人” - 这就是我所指的。 – biphobe 2016-03-16 12:29:20

10

要包括所有的文件和目录:

archive.bulk([ 
    { 
    expand: true, 
    cwd: "temp/freewheel-bvi-120", 
    src: ["**/*"], 
    dot: true 
    } 
]); 

它使用节点水珠(https://github.com/isaacs/node-glob)的下方,所以与兼容任何匹配的表达式会工作。

2

管道结果响应对象(场景中有一个需要下载的zip,而不是存储在本地)

archive.pipe(res); 

山姆提示访问为我工作的目录的内容。

src: ["**/*"] 
7

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(); 
+2

想知道这一点,他们说批量已被弃用,但没有建议使用哪个函数来代替。 – 2017-03-13 07:46:24