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

2019-05-16 11:03发布

问题:

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?

回答1:

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() {
    console.log(data);
  });
}).on('error', function(e) { });


标签: node.js gzip