Working with ZIP Archives in Node.js: The Adm-Zip Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Adm-Zip Library for Working with ZIP Archives in Node.js

When developing web applications, server utilities, or data processing tools on Node.js, there is often a need to work with ZIP archives: packaging files before sending them to a user, unpacking received archives, or manipulating contents without full extraction. In the JavaScript ecosystem, there are several libraries for these tasks, and one of the most popular and easy to use is Adm-Zip.



What is Adm-Zip?

Adm-Zip is a pure JavaScript library for Node.js that allows you to create, read, and modify ZIP archives without external dependencies (for example, without installing the zip or unzip utility on the system). It works both with files on disk and with buffers in memory, making it ideal for web servers and cloud functions.



Main features of the library:

  • Creating new ZIP archives from files, folders, or string data.
  • Extracting archives to a specified directory or into a buffer.
  • Reading archive contents without full extraction (viewing file list, reading individual entries).
  • Adding, deleting, and updating files inside an existing archive.
  • Working with comments for the archive and individual files.
  • Password support (ZipCrypto encryption).
  • Working with streams for processing large data.


Installation

Installing the library is standard for npm packages:

npm install adm-zip


After installation, you can include it in your project:

const AdmZip = require('adm-zip');


Creating a ZIP Archive

Creating a new archive is very simple. You can add both individual files and entire folders.



const AdmZip = require('adm-zip');

// Create a new archive instanceconst zip = new AdmZip();

// Add a file from a stringzip.addFile('hello.txt', Buffer.from('Hello, World!', 'utf8'));

// Add a file from diskzip.addLocalFile('/path/to/image.jpg');

// Add the contents of a local folderzip.addLocalFolder('/path/to/documents', 'backup/docs');

// Write the archive to diskzip.writeZip('myarchive.zip');

console.log('Archive created: myarchive.zip');


You can also get the archive as a buffer for sending via an HTTP response:

const zipBuffer = zip.toBuffer();// Now zipBuffer can be sent to the user as a binary file


Extracting a ZIP Archive

Extraction is done in a few lines. The library supports both extracting all files and reading individual entries.



const AdmZip = require('adm-zip');

try { // Open an existing archive const zip = new AdmZip('myarchive.zip');

// Extract all files to the specified folder zip.extractAllTo('extracted/', true); // true — overwrite existing files

console.log('Archive extracted to folder extracted/');} catch (err) { console.error('Error during extraction:', err.message);}


If you need to read the archive contents without extraction:

const zip = new AdmZip('myarchive.zip');

// Get an array of entries (files and folders)const zipEntries = zip.getEntries();

zipEntries.forEach(entry => { if (!entry.isDirectory) { console.log(`File: ${entry.entryName}, size: ${entry.header.size} bytes`); // Read the file content as a string const content = entry.getData().toString('utf8'); console.log('Content:', content.substring(0, 100)); }});


Working with Passwords (Encryption)

Adm-Zip supports ZipCrypto encryption. Please note: this is not the most modern method (AES is not supported), but it is suitable for basic protection.



<

Recommendations