在nodejs中解压缩文件夹时出现无效签名错误

invalid signature error when unzip folder in nodejs

我使用解压缩节点模块来解压缩我的 binary-data(来自 request 模块)。 在某些情况下,当 request 模块的 response 不包含 zip 文件夹 binary data(如果响应没有 zip 文件夹数据,一些其他二进制数据)时,它会失败。

How I handle this exception.

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);
    });

zip 解压:

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.

使用 adm-zip 模块处理异常。

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

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

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