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?
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.
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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With