简体   繁体   中英

How to extract a ZIP file from a HTTP resource with node.js

How to extract a ZIP file from a HTTP resource with node.js?

I tried the the below code but got an error indicating the ZIP file may be corrupt (which it isn't):

require('node-zip');
var request = require('request');

request('http://data.geo.admin.ch.s3.amazonaws.com/ch.meteoschweiz.swissmetnet/data.zip', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(new JSZip(body, {base64: false, checkCRC32: true}).files);
  }
})

Error:

Error: End of data reached (data length = 230973, asked index = 261475). Corrupted zip ?

request tries to decode the content as text, corrupting your data. Try with :

request({
  method : "GET",
  url : "http://data.geo.admin.ch.s3.amazonaws.com/...",
  encoding: null // <- this one is important !
}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(new JSZip(body).files);
    // handle error
    return;
  }
});

See also http://stuk.github.io/jszip/documentation/howto/read_zip.html (the request example at the bottom of the page).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM