问题
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);
}
});
This code gives me error:
Can not create zip, bad data Invalid CEN header (bad signature)
I am using Windows 7. I can't even open this ZIP file with 7-zip or WinRAR (simple error like corrupted data).
Also, utils.apiRequest
function body is:
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: I don't have problems using curl
:)
回答1:
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
});
Since a zip file is binary you should use a Buffer.
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;
}
回答2:
(Adding as an answer so I can post the code snippet)
@vincent is on the right track I think - sounds like you're not writing the data as binary to the file. It's often easier to just pipe a download request straight to a file:
var http = require('http');
var fs = require('fs');
var AdmZip = require('adm-zip')
var tmpZipStream = fs.createWriteStream('bob.zip');
var request = http.get('http://example.com/example.zip', function(response) {
response.pipe(tmpZipStream);
});
tmpZipStream.on('close', function() {
var zip;
try {
zip = new AdmZip('bob.zip');
} catch (e) {
console.log('Can not create zip, bad data', e);
}
})
Without knowing where utils.apiRequest comes from it's hard to say if this is workable for you, but hopefully it helps.
来源:https://stackoverflow.com/questions/38466944/can-not-open-zip-file-after-downloading-through-nodejs-application-because-of-in