我使用解压缩节点模块解压缩binary-data
(来自request
模块)。
在某些情况下request
模块的response
不包含zip文件夹binary data
(如果响应没有zip文件夹数据,其他一些二进制数据),则会失败。
我如何处理此异常。
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 :(得分:0)
使用adm-zip模块处理异常。
const admzip = require('adm-zip');
try {
var zip = new admzip(zipData);
zip.extractAllTo(/*path*/);
}
catch (exception) {
console.error(exception);
}