Search code examples
node.jsgzipunzipfs

Unzip file from disk and read data from it on node.js


I need read unzipped file to string. I added these lines.

fs.readFile("report.gz", function(err, data) {
    zlib.gunzip(data, function(err, dezipped) {
      if (err) {
        console.log(err);
      } else {
        console.log('length of data = ' + data.length);
        console.log('length of dezipped =' + dezipped.length);
        console.log(dezipped.toString("utf-8"));
      }
    });
  });

As a result I have only first line from my file (in dezipped variable), how can I read all lines ?

In console I see

length of data = 88875 (~ 85 Kb)
Length of dezipped = 528 (bytes)

I can conclude that dezipped are alredy cutted data.


Solution

  • I'm not sure if this is the case in your situation, but I can reproduce something similar by combining multiple gzip blocks into one file (which is perfectly valid AFAIK):

    $ { echo ONE | gzip; echo TWO | gzip } > output.gz
    $ gzcat output.gz
    ONE
    TWO
    

    zlib will only extract the first block:

    $ node app.js
    length of data = 48
    length of dezipped =4
    ONE
    

    I found that zlibjs will handle these files properly, though:

    $ node app.js
    length of data = 48
    length of dezipped =8
    ONE
    TWO
    

    It's a drop-in replacement, so this is all you need to do for your code to work:

    var zlib = require('zlibjs');
    

    Since it's pure JS, it will probably not be as fast, though.