Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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

Problem

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?

Problem courtesy of: Fluffy

Solution

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) { });
Solution courtesy of: fent

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

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

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×