Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  41] [ 7]  / answers: 1 / hits: 16688  / 7 Years ago, fri, august 11, 2017, 12:00:00

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?


More From » node.js

 Answers
40

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

[#56796] Tuesday, August 8, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
teagan

Total Points: 98
Total Questions: 106
Total Answers: 101

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;