Categories
JavaScript Answers

How to download and unzip a zip file in memory in Node?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *