Answer the question
In order to leave comments, you need to log in
How to get json data from archive in Node.js?
There is a "lib.txt" file. It is located in the "lib.txt.gz" archive.
This file contains a regular json object, like this:
var a = [
{a:1},
{b:2}
]
Answer the question
In order to leave comments, you need to log in
const zlib = require('zlib');
const fs = require('fs');
/**
* Read the gz compresed file
* @param filename (string) full path to file
* @return (Promise<Buffer>)
*/
function readGZipFile(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, (err, buf) => {
if(err) {
reject(err);
return;
}
zlib.gunzip(buf, (err, buf) => {
if(err) {
reject(err);
return;
}
resolve(buf);
});
});
});
}
//Использование
const path = require('path');
readGZipFile(path.resolve(__dirname, 'lib.txt.gz')).then(buf => JSON.parse(buf.toString())).then(data => {
//Ваш код для работы с data
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question