2016-12-06 278 views
2

我使用解壓縮節點模塊解壓縮我的binary-data(來自request模塊)。 它在某些情況下,當request模塊的response不包含zip文件夾binary data(如果響應沒有zip文件夾數據,一些其他二進制數據)失敗。nodejs中解壓縮文件夾時出現無效簽名錯誤

我如何處理這個異常。

const request = require("request"); 
const unzip = require('unzip'); 
const stream = require('stream'); 

var options = { 
     method: 'GET', 
     url: /*URL*/, 
     encoding: null 
    }; 

    request(options, function (error, response, body) { 
     zipExtract(error, body); 
    }); 

zipExtract:

function zipExtract(error, zipData) { 
    if (error) { 
     console.error(error); 
    } 
    else { 
     try { 
      //create stream object 
      var artifactStream = new stream.PassThrough(); 

      //parse buffer into stream 
      artifactStream.end(zipData); 

      //pipe response to unzip 
      artifactStream.pipe(unzip.Extract({path: 'app/output'})); 
     } 

     catch (exception) { 
      console.error(exception); 
     } 
    } 
} 

它提示錯誤在控制檯上的處理異常

events.js:160 
     throw er; // Unhandled 'error' event 
    ^

Error: invalid signature: 0x6d74683c 
    at C:\app-hub\module-application-size\node_modules\unzip\lib\parse.js:63:13 
    at runCallback (timers.js:637:20) 
    at tryOnImmediate (timers.js:610:5) 
    at processImmediate [as _immediateCallback] (timers.js:582:5) 
npm ERR! Test failed. See above for more details. 

回答

0

使用ADM-ZIP模塊。

const admzip = require('adm-zip'); 

try { 
    var zip = new admzip(zipData); 
    zip.extractAllTo(/*path*/); 

} 
catch (exception) { 
    console.error(exception); 
}