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
:)
The problem is that you're decoding the received data into an utf8 string:
Since a zip file is binary you should use a Buffer.
Here is an example replacement for your
utils.apiRequest
with Buffer:(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:
Without knowing where utils.apiRequest comes from it's hard to say if this is workable for you, but hopefully it helps.