I asked myself a question,
I can read files (csv mainly) on a cloud platform but when it's a zip I just get a bunch of:
j�\lȜ��&��3+xT��J��=��y��7���vu� {d�T���?��!�
Which is normal, so I wonder if there is a way to put that in a variable and unzip it using a lib or something like that.
Thanks for your time
I asked myself a question,
I can read files (csv mainly) on a cloud platform but when it's a zip I just get a bunch of:
j�\lȜ��&��3+xT��J��=��y��7���vu� {d�T���?��!�
Which is normal, so I wonder if there is a way to put that in a variable and unzip it using a lib or something like that.
Thanks for your time
Share Improve this question edited Nov 26, 2018 at 12:53 Amiga500 6,14111 gold badges71 silver badges119 bronze badges asked Nov 26, 2018 at 12:46 Quentin_otdQuentin_otd 2331 gold badge5 silver badges19 bronze badges3 Answers
Reset to default 3You should use jszip npm package. This allows you to quickly read zip files.
Example:
var fs = require("fs");
var JSZip = require("jszip");
// read a zip file
fs.readFile("project.zip", function(err, data) {
if (err) throw err;
JSZip.loadAsync(data).then(function (zip) {
files = Object.keys(zip.files);
console.log(files);
});
});
To read the contents of a file in the zip archive you can use the following.
// read a zip file
fs.readFile("project.zip", function(err, data) {
if (err) throw err;
JSZip.loadAsync(data).then(function (zip) {
// Read the contents of the 'Hello.txt' file
zip.file("Hello.txt").async("string").then(function (data) {
// data is "Hello World!"
console.log(data);
});
});
});
and to download the zip file from the server:
request('yourserverurl/helloworld.zip')
.pipe(fs.createWriteStream('helloworld.zip'))
.on('close', function () {
console.log('File written!');
});
you should use npm install node-stream-zip
const StreamZip = require('node-stream-zip');
const zip = new StreamZip({
file: 'archive.zip',
storeEntries: true
});
and get the info like this
zip.on('ready', () => {
console.log('Entries read: ' + zip.entriesCount);
for (const entry of Object.values(zip.entries())) {
const desc = entry.isDirectory ? 'directory' : `${entry.size} bytes`;
console.log(`Entry ${entry.name}: ${desc}`);
}
// Do not forget to close the file once you're done
zip.close()
});
Hope it helps :-)
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);
});
});