Reputation: 449
How can I read a .gz file into an array in nodejs without having to unzip?
It is simple to do in PHP by using "gzfile", is there an equivalent in nodejs?
I attempted with this code below but myarray does not print out and I am not sure it is the best method:
const fs = require('fs');
const zlib = require('zlib');
const readline = require('readline');
let lineReader = readline.createInterface({
input: fs.createReadStream('myfile.gz').pipe(zlib.createGunzip())
});
let n = 0;
var myarray =[];
lineReader.on('line', (line) => {
myarray[n]=line;
n += 1
console.log("line: " + n);
console.log(line);
});
console.log(myarray);
Upvotes: 1
Views: 816
Reputation: 5995
This is because console.log(myarray)
is executed even before any 'line'
event has come and a blank array is printed in console. You could promisify the event listener but I guess you would only be able to await
the first event. You could use an alternative solution to read the lines into myarray
which is based on the example in the official nodejs doc.
const fs = require('fs');
const zlib = require('zlib');
const readline = require('readline');
const lineF = (path) => readline.createInterface({
input: fs.createReadStream(path).pipe(zlib.createGunzip()),
crlfDelay: Infinity
});
var myarray = [];
const myFunction = async () => {
for await (const line of lineF("myfile.gz")) {
myarray.push(line);
}
}
(async () => {
await myFunction();
console.log(myarray);
})();
Upvotes: 1