由于无效的CEN错误,通过nodejs应用程序下载后无法打开zip文件 [英] Can not open zip file after downloading through nodejs application because of Invalid CEN error
问题描述
我需要在我的nodejs应用程序中下载并解压缩zip归档文件.我有以下代码:
I need to download and unzip zip archive in my nodejs application. I have this code:
utils.apiRequest(teamcityOptions)
.then(function (loadedData) {
var tempDir = tmp.dirSync();
var tmpZipFileName = tempDir.name + "\\" + 'bob.zip';
fs.appendFileSync(tmpZipFileName, loadedData);
var zip;
try {
zip = new AdmZip(tmpZipFileName);
} catch (e) {
log('Can not create zip, bad data', e);
}
});
此代码给我错误:
无法创建zip,数据错误CEN标头无效(签名错误)
Can not create zip, bad data Invalid CEN header (bad signature)
我正在使用Windows7.我什至无法使用7-zip或WinRAR(诸如损坏的数据之类的简单错误)打开此ZIP文件.
I am using Windows 7. I can't even open this ZIP file with 7-zip or WinRAR (simple error like corrupted data).
此外,utils.apiRequest
函数主体为:
utils.apiRequest: function (options) {
var deferred = defer();
https.get(options, function (request) {
var loadedData = '';
request.on('data', function (dataBlock) {
loadedData += dataBlock.toString('utf8');
});
request.on('end', function () {
deferred.resolve(loadedData);
})
});
return deferred.promise;
}
我该如何解决我的问题?
How can I solve my problem?
PS:使用curl
没问题:)
PS: I don't have problems using curl
:)
推荐答案
问题是您将接收到的数据解码为utf8字符串:
The problem is that you're decoding the received data into an utf8 string:
request.on('data', function (dataBlock) {
loadedData += dataBlock.toString('utf8'); // this is wrong
});
由于zip文件是二进制文件,因此您应该使用Buffer.
Since a zip file is binary you should use a Buffer.
以下是使用缓冲区替换utils.apiRequest
的示例:
Here is an example replacement for your utils.apiRequest
with Buffer:
utils.apiRequest: function (options) {
var deferred = defer();
https.get(options, function (request) {
var data = [];
request.on('data', function (dataBlock) {
data.push(dataBlock);
});
request.on('end', function () {
deferred.resolve(Buffer.concat(data));
});
});
return deferred.promise;
}
这篇关于由于无效的CEN错误,通过nodejs应用程序下载后无法打开zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!