kgangadhar
kgangadhar

Reputation: 5088

Unable to unzip the zipped files using zlib module

I am trying to unzip the files from the zipped data using node build-in module zlib, for some reason I am not able to unzip it, I am getting an error as follows:

Error: incorrect header check
test.js:53
No debug adapter, can not send 'variables'

The code I am trying is as follows:

var zlib = require('zlib');
var fs = require('fs');
var filename = './Divvy_Trips_2019_Q2.zip';

var str1 = fs.createReadStream(filename);

var gzip = zlib.createGunzip();

str1.pipe(gzip).on('data', function (data) {
    console.log(data.toString());
}).on('error', function (err) {
    console.log(err);
});

The URL to the zipped data is as follows: Divvy_Trips_2019_Q2.zip

Upvotes: 0

Views: 732

Answers (1)

Lambda Fairy
Lambda Fairy

Reputation: 14734

GZip (.gz) and ZIP (.zip) are different formats. You need a library that handles ZIP files, like yauzl.

// https://github.com/thejoshwolfe/yauzl/blob/master/examples/dump.js

const yauzl = require("yauzl");

const path = "./Divvy_Trips_2019_Q2.zip";

yauzl.open(path, function(err, zipfile) {
  if (err) throw err;
  zipfile.on("error", function(err) {
    throw err;
  });
  zipfile.on("entry", function(entry) {
    console.log(entry);
    console.log(entry.getLastModDate());
    if (/\/$/.exec(entry)) return;
    zipfile.openReadStream(entry, function(err, readStream) {
      if (err) throw err;
      readStream.pipe(process.stdout);
    });
  });
});

Upvotes: 1

Related Questions