Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read gzip stream line by line

Tags:

node.js

gzip

zlib

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?

like image 898
Markus Avatar asked Jun 28 '16 10:06

Markus


People also ask

How do I read a gzip file?

Launch WinZip from your start menu or Desktop shortcut. Open the compressed file by clicking File > Open. If your system has the compressed file extension associated with WinZip program, just double-click on the file.

How do I unzip a GZ file in Linux?

You can unzip GZ files in Linux by adding the -d flag to the Gzip/Gunzip command. All the same flags we used above can be applied. The GZ file will be removed by default after we uncompressed it unless we use the -k flag. Below we will unzip the GZ files we compressed in the same directory.


1 Answers

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);
});
like image 168
robertklep Avatar answered Oct 23 '22 14:10

robertklep