Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Zlib incorrect header check

I am trying to unzip a gzipped file in Node but I am running into the following error.

Error: incorrect header check at Zlib._handle.onerror (zlib.js:370:17)

Here is the code the causes the issue.

'use strict'

const fs = require('fs');
const request = require('request');
const zlib = require('zlib');
const path = require('path');

var req = request('https://wiki.mozilla.org/images/f/ff/Example.json.gz').pipe(fs.createWriteStream('example.json.gz'));

req.on('finish', function() {
    var readstream = fs.createReadStream(path.join(__dirname, 'example.json.gz'));
    var writestream = fs.createWriteStream('example.json');

    var inflate = zlib.createInflate();
    readstream.pipe(inflate).pipe(writestream);
});
//Note using file system because files will eventually be much larger

Am I missing something obvious? If not, how can I determine what is throwing the error?

like image 240
MikeV Avatar asked Aug 11 '17 18:08

MikeV


1 Answers

The file is gzipped, so you need to use zlib.Gunzip instead of zlib.Inflate.

Also, streams are very efficient in terms of memory usage, so if you want to perform the retrieval without storing the .gz file locally first, you can use something like this:

request('https://wiki.mozilla.org/images/f/ff/Example.json.gz')
  .pipe(zlib.createGunzip())
  .pipe(fs.createWriteStream('example.json'));

Otherwise, you can modify your existing code:

var gunzip = zlib.createGunzip();
readstream.pipe(gunzip).pipe(writestream);
like image 61
robertklep Avatar answered Oct 02 '22 08:10

robertklep