admin管理员组

文章数量:1308812

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('.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?

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/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?

Share Improve this question asked Aug 11, 2017 at 18:24 MikeVMikeV 6571 gold badge8 silver badges21 bronze badges 1
  • In my case it was a wrong function usage. E.g. inflateSync while inflateRawSync was needed. Working example: gist.github./vgorloff/597d840f1b2a915b88b36c342cf56576 – Vlad Commented Jul 14, 2021 at 22:50
Add a ment  | 

1 Answer 1

Reset to default 6

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/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);

本文标签: javascriptNode Zlib incorrect header checkStack Overflow