Working with ZIP Archives in Node.js: archiver and adm-zip Libraries
In modern web development with Node.js, there is often a need to package files into archives or extract data from them. Whether it's backup, downloading multiple files in a single archive, or processing user data — working with the ZIP format remains in demand. There are several popular libraries for Node.js, and in this article we will look at two of the most powerful and convenient ones: archiver (for creating archives) and adm-zip (for reading and manipulating archives).
Why are libraries for working with ZIP needed?
Node.js does not provide built-in tools for working with archives by default. Although the zlib module exists for compressing individual streams, creating full-fledged ZIP files with multiple files and folders requires specialized solutions. The archiver and adm-zip libraries solve these tasks by offering a simple API and high performance.
Installing the Libraries
Both libraries are installed via npm. Run in the terminal:
npm install archiver adm-zipThe archiver Library: Creating ZIP Archives
archiver is a library for creating archives in various formats (ZIP, TAR, GZIP). It works with streams, which allows efficient processing of large amounts of data without loading the entire file into memory.
Key features of archiver:
- Creating ZIP archives with configurable compression level
- Adding files and entire directories
- Working with streams for memory efficiency
- Support for comments and metadata
Example: creating a simple ZIP archive
const fs = require('fs');const archiver = require('archiver');
// Create a write stream to a fileconst output = fs.createWriteStream('example.zip');const archive = archiver('zip', { zlib: { level: 9 } }); // maximum compression
// Handle eventsoutput.on('close', () => { console.log(`Archive created: ${archive.pointer()} bytes`);});
archive.on('error', (err) => { throw err;});
// Add filesarchive.append('File content 1', { name: 'file1.txt' });archive.file('/path/to/existing/file.txt', { name: 'renamed-file.txt' });archive.directory('my_folder/', 'new_folder_name');
// Finalize the archivearchive.pipe(output);archive.finalize();Example: archiving with streaming
const http = require('http');const archiver = require('archiver');
http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'application/zip', 'Content-disposition': 'attachment; filename=download.zip' });
const archive = archiver('zip', { zlib: { level: 5 } }); archive.pipe(res);
archive.append('Text file', { name: 'text.txt' }); archive.append(Buffer.from([0x00, 0x01, 0x02]), { name: 'binary.bin' }); archive.finalize();}).listen(3000);The adm-zip Library: Reading and Extracting Archives
adm-zip is a versatile library for working with ZIP archives: reading, writing, updating, and extracting. Unlike archiver, it works not only with streams but also with buffers, which is convenient for small archives.
Key features of adm-zip:
- Reading existing ZIP files
- Extracting all or individual files
- Writing new archives (alternative to archiver)
- Working with buffers and strings
Example: extracting files from an archive
const AdmZip = require('adm-zip');
// Load an existing archiveconst zip = new AdmZip('./example.zip');
// Get the list of filesconst zipEntries = zip.getEntries();zipEntries.forEach((entry) => { console.log(`File