Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not open zip file after downloading through nodejs application because of Invalid CEN error

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 :)

like image 503
Sharikov Vladislav Avatar asked Oct 21 '25 04:10

Sharikov Vladislav


1 Answers

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;
}
like image 110
vincent Avatar answered Oct 23 '25 20:10

vincent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!