To download and unzip a zip file in memory in Node, we use the adm-zip
module.
To install it, we run
npm install adm-zip
Then we write
const fileUrl =
"https://github.com/mihaifm/linq/releases/download/3.1.1/linq.js-3.1.1.zip";
const AdmZip = require("adm-zip");
const request = require("request");
request.get({ url: fileUrl, encoding: null }, (err, res, body) => {
const zip = new AdmZip(body);
const zipEntries = zip.getEntries();
console.log(zipEntries.length);
zipEntries.forEach((entry) => {
if (entry.entryName.match(/readme/i)) console.log(zip.readAsText(entry));
});
});
to call request.get
with an object with the url
of the file to unzip.
We get the zip file’s content from body
.
And then we call getEntries
to get the items in the zip file.