2014-01-27 310 views
5

我现在使用zlib和fstream一起压缩并发送到客户端,现在我需要解压缩一个存档(可能包含子文件夹)到维护文件夹结构的文件夹中。我怎么做?如何将Node.js中的.zip/.rar文件解压缩到文件夹中

+0

你好我可以知道如何将整个文件夹压缩成一个zip文件。我尝试使用fstream.Reader({path:“E:\\ d data \\ electron \\ Applications \\ FirstApp \\ js \\ temp \\”,键入:“Directory”}) \t .pipe新的tar.Pack()) \t .pipe(zlib.Gzip()) \t .pipe(fstream.Writer({path:“C:\\ Users \\ Raina \\ AppData \\ Local \\ Temp”, file:'compressed_folder.tar.gz'}));但我得到一个错误。 –

回答

9

有很多节点模块可以为你做到这一点。其中之一是节点解压缩。您可以像这样简单地将.zip文件提取到目录。

fs.createReadStream('path/to/archive.zip').pipe(unzip.Extract({ path: 'output/path' }));

进一步阅读:https://github.com/EvanOxfeld/node-unzip

+0

我可以使用zlib而不使用unzip吗? – Raghavendra

+0

@Raghav我相信zlib是用于处理GZip而不是Zip。 – gpopoteur

+0

@gpopoteur嗨,我知道这篇文章是相当古老的,但它对我很有帮助。你能告诉我如何知道什么时候fs.createReadStream('path/to/archive.zip')。pipe(unzip.Extract({path:'output/path'}));完成?完成后我想做点什么:) –

0

的RAR是一个封闭的代码的软件。你能做到这一点的唯一方法 - 安装命令行RAR(RAR,这是可以在大多数平台上的RAR.EXE或Linux版本),并通过这种方式称之为:

var exec = require('child_process').exec; 

exec("rar.exe x file.rar", function (error) { 
    if (error) { 
    // error code here 
    } else { 
     // success code here 
    } 
}); 
1

您可以使用此令人赞叹的模块http://node-machine.org/machinepack-zip

的解压与目录结构的zip文件内袋

var Zip = require('machinepack-zip'); 

//解压缩指定的.zip文件和写解压缩的文件/目录作为指定的目标目录的内容。

Zip.unzip({ 
    source: '/Users/mikermcneil/stuff.zip', 
    destination: '/Users/mikermcneil/my-stuff', 
}).exec(callbackSuccess, callbackFail); 

下载远程文件并解压您可以使用此代码:

 var fs = require('fs'); 
    var unzip = require("unzip2"); 
    var tar = require('tar'); 
    var zlib = require('zlib'); 
    var path = require('path'); 
    var mkdirp = require('mkdirp'); // used to create directory tree 
    var request = require("request"); 
    var http = require('http'); 
    var zip = require("machinepack-zip"); 



    for (var i = 0; i < _diff.length; i++) { 
     request(constants.base_patch +"example.zip") 
      request = http.get({ host: 'localhost', 
            path: '/update/patchs/' + "example.zip", 
            port: 80, 
            headers: { 'accept-encoding': 'gzip,deflate' } }); 

      request.on('response', (response) => { 
       var output = fs.createWriteStream(__dirname + "/tmp/" +"example.zip"); 

        switch (response.headers['content-encoding']) { 
        // or, just use zlib.createUnzip() to handle both cases 
        case 'gzip': 
         response.pipe(zlib.createGunzip()).pipe(unzip.Extract({ path: __dirname })); 
         break; 
        case 'deflate': 

         response.pipe(zlib.createInflate()).pipe(unzip.Extract({ path: __dirname })); 
         break; 
        default: 
         response.pipe(output); 
         break; 
        } 
      }) 

      request.on('close', function(){ 
       zip.unzip({ 
        source: __dirname + "/tmp/" + "example.zip", 
        destination: __dirname, 
       }).exec({ 
        error: function (err){ 
        alert("error") 
        }, 

        success: function(){ 
        //delete temp folder content after finish uncompress 
        }, 
       }); 
      }) 
    } 

注:去掉unnecesary模块。