Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to gunzip chunk-by-chunk in node.js?

Tags:

node.js

gzip

I'm getting contents of a gzipped web page in chunks and want to decompress each as soon as it gets received, so I'm trying to do (stuff stripped for readability):

var decompress = function(string, callback) {
    zlib.gunzip(string, callback);
};

decompress(chunk, function(data) {
    console.log(data);
});

However I'm only getting nulls logged to the console. My node version is 0.6.2 and zlib is the built-in one. How should I decompress it?

like image 232
Fluffy Avatar asked Sep 02 '25 09:09

Fluffy


1 Answers

If you want to pipe contents to Gunzip, use zlib#createGunzip()

http.get(options, function(res) {
  var gunzip = zlib.createGunzip();
  res.pipe(gunzip);
  gunzip.on('data', function(data) {
    console.log(data);
  });
}).on('error', function(e) {
    console.error(e)
});
like image 100
fent Avatar answered Sep 04 '25 10:09

fent