Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Thu Mar 30 2023

How to create and read from a zip file in NodeJs

In this guide we will take a look at how we can create zip files in NodeJs, and how we can decompress and get the data out of a zip file. A scenario where you need to be able to create a zip file could be e.g if you are working on a company with large amount of user data, and the user perhaps asks to extract all the data from the company and get it sent to him/her, then a zip file could be a wise choice. Or, you just want to learn how to do some cool zip files in NodeJs - regardless of your situation, tag along and you will learn how to do it!

Setup

  • Create zip.js file
  • Enter npm install adm-zip

Create zip file

Let start with writing our ZipHandler class which will have a function for saving files and compress to a zip file and save it locally to the disk

const AdmZip = require("adm-zip"); class ZipHandler { // Files will be an array containing objects defined like: { name: string, buffer: Buffer } createZipFile(files = [], zipPath) { const zip = new AdmZip(); files.forEach((f) => { zip.addFile(f.name, f.buffer); }); zip.writeZip(zipPath); } }

Let's test it out with some local files. Final code:

const AdmZip = require("adm-zip"); const fs = require("fs"); class ZipHandler { // Files will be an array containing objects defined like: { name: string, buffer: Buffer } createZipFile(files = [], zipPath) { const zip = new AdmZip(); files.forEach((f) => { zip.addFile(f.name, f.buffer); }); zip.writeZip(zipPath); } } (() => { const files = []; ["cat.png", "test.txt"].forEach((name) => { const buffer = fs.readFileSync(name); files.push({ name, buffer }); }); const zipHandler = new ZipHandler(); zipHandler.createZipFile(files, "myCoolZip.zip"); })();

Run code

node zip.js

In your project folder, it should look like this:

Screenshot

And if you decompress the zip, your files should be in the folder.

Get files from the zip

Now let us do the reverse, we will now read the files back from the zip file and save them to disk with a new name:

getFilesFromZip(zipPath) { const files = []; const zip = new AdmZip(zipPath); zip.getEntries().forEach((entry) => files.push({ name: `zip-${entry.name}`, buffer: entry.getData(), }) ); return files; }

And to try it out, final code look like this:

const AdmZip = require("adm-zip"); const fs = require("fs"); class ZipHandler { // Files will be an array containing objects defined like: { name: string, buffer: Buffer } createZipFile(files = [], zipPath) { const zip = new AdmZip(); files.forEach((f) => { zip.addFile(f.name, f.buffer); }); zip.writeZip(zipPath); } // Returns an array containing objects defined like: { name: string, buffer: Buffer } getFilesFromZip(zipPath) { const files = []; const zip = new AdmZip(zipPath); zip.getEntries().forEach((entry) => files.push({ name: `zip-${entry.name}`, buffer: entry.getData(), }) ); return files; } } (() => { const zipHandler = new ZipHandler(); const files = zipHandler.getFilesFromZip("myCoolZip.zip"); files.forEach((file) => fs.writeFileSync(file.name, file.buffer)); })();

Run code

node zip.js

Result

Result

Now we should have duplicated files, so the files from zip got a new name (zip as prefix).

Outro

All right, so here we have a simple example of how we can create zip files in NodeJs, and to read it back and save to disk again. If you have a REST API and want to be able to send data to your API, you can connect your knowledge learned from this guide with this guide where we go through how we can create endpoints to send files to an endpoint.

Over and out!

signatureThu Mar 30 2023
See all our articles