Working with ZIP Archives in Node.js: A Complete Guide

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

Working with ZIP Archives in Node.js: adm-zip and archiver Libraries



In modern web development with Node.js, there is often a need to work with archives: package logs, transfer files in groups, extract user-uploaded data. In this material, we will look at two main libraries for working with the ZIP format — adm-zip and archiver. They solve almost all tasks related to archiving and are well-suited for both simple scripts and production applications.



adm-zip Library: A Universal Tool for Reading and Writing ZIP



adm-zip is a pure JavaScript implementation for working with ZIP archives without external dependencies. It allows you to both create new archives and read/modify existing ones. The library is simple to use and does not require installing additional system utilities.



Installation

npm install adm-zip


Creating a ZIP Archive

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

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

// Add files from buffer or stringzip.addFile('hello.txt', Buffer.from('Hello, World!', 'utf8'));

// Add a local filezip.addLocalFile('/path/to/image.png');

// Add an entire folderzip.addLocalFolder('/path/to/documents', 'docs');

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

console.log('Archive created successfully!');


Reading and Extracting an Existing Archive

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

// Load an existing archiveconst zip = new AdmZip('./input/archive.zip');

// Get the list of filesconst zipEntries = zip.getEntries();zipEntries.forEach((entry) => { console.log(`File: ${entry.entryName}, size: ${entry.header.size} bytes`);});

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

// Or get the contents of a specific fileconst content = zip.readAsText('data.json');console.log('Contents of data.json:', content);


Working with Buffers (Without Writing to Disk)

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

zip.addFile('report.csv', 'id,name,age1,John,30');

// Get the archive as a Buffer for sending via HTTPconst archiveBuffer = zip.toBuffer();

// Now archiveBuffer can be sent as a server response// or passed to another function


archiver Library: Advanced Streaming Archiving



archiver is a more powerful library optimized for working with large amounts of data. It uses Node.js streams, which allows archiving files "on the fly" without loading the entire content into memory. This is an ideal choice for server applications with limited resources.



Installation

npm install archiver


Creating an Archive with Streaming Output

const archiver = require('archiver');const fs = require('fs');const path = require('path');

// Create a write streamconst output = fs.createWriteStream('./backup.zip');const archive = archiver('zip', { zlib: { level: 9 } // Maximum compression level});

// Event handlingoutput.on('close', () => { console.log(`Archive created: ${archive.pointer()} bytes`);});

archive.on('error', (err) => { throw err;});

// Pipe the archive stream to the output filearchive.pipe(output);

// Add filesarchive.file('/var/log/app.log', { name: 'logs/app.log' });

// Add a directory recursivelyarchive.directory('uploads/', 'user_files');

// Add a string as a filearchive.append('This is the file content', { name: 'readme.txt' });

// Finalize the archivingarchive.finalize();


Sending an Archive via HTTP Response

const express = require('ex                    

Recommendations