Read gzip stream line by line

Markus picture Markus · Jun 28, 2016 · Viewed 8.7k times · Source

I've got a compressed gzip file which I would like to read line by line.

var fs = require('fs')
var zlib = require('zlib')
var gunzip = zlib.createGunzip()
var inp = fs.createReadStream('test.gz')
var n = 0

var lineProcessing = function (err, data) {
    if (!err) {
        n += 1
        console.log ("line: " + n)
        console.log (data.toString())
    }
}

inp
  .on('data', function (chunk) {
      zlib.gunzip (chunk, lineProcessing)
  })
  .on('end', function () {
    console.log ('ende');
  });

I guess I need to set a chunksize for zlib.createGunzip that I only read until the next \n. But how to determine it dynamically?

Answer

robertklep picture robertklep · Jun 28, 2016

It might be easier to use readline for this:

const fs       = require('fs');
const zlib     = require('zlib');
const readline = require('readline');

let lineReader = readline.createInterface({
  input: fs.createReadStream('test.gz').pipe(zlib.createGunzip())
});

let n = 0;
lineReader.on('line', (line) => {
  n += 1
  console.log("line: " + n);
  console.log(line);
});