Scenario1: If an API's response is a zipped file (E.x. Few Microsoft Graph APIs response is a zipped file), you can use npm
unzipper & request packages to extract data to an object.
const unzipper = require('unzipper');
const request = require('request');
//Read zip file as stream from URL using request.
const responseStream = request.get({ url: ''});
let str = '';
responseStream.on('error', (err) => {
if (err) { console.error(err); throw err; }
});
responseStream.pipe(unzipper.Parse())
.on('entry', (entry) => {
entry.on('data', (chunk) => {
//Convert buffer to string (add trim to remove any unwanted spaces) & append to existing string at each iteration.
str += chunk.toString().trim();
}).on('end', () => {
const respObj = JSON.parse(str); //At the end convert the whole string to JSON object.
console.log(respObj);
});
});
Ref: read-zipped-file-content-using-nodejs
Scenario2: If you wanted to read zipped file from a server i.e., local.
const unzipper = require('unzipper');
const fs = require('fs');
const readStream = fs.createReadStream(`filePath`);
let str = '';
readStream.on('error', (err) => {
if (err) { console.error(err); throw err; }
});
readStream.pipe(unzipper.Parse())
.on('entry', (entry) => {
entry.on('data', (chunk) => {
//Convert buffer to string (add trim to remove any unwanted spaces) & append to existing string at each iteration.
str += chunk.toString().trim();
}).on('end', () => {
const respObj = JSON.parse(str); //At the end convert the whole string to JSON object.
console.log(respObj);
});
});