2013-03-26 29 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