What is adm-zip?
The adm-zip npm package is a JavaScript library for ZIP archive manipulation. It allows users to work with ZIP files directly from their Node.js applications. With adm-zip, you can create, extract, and update ZIP archives without relying on external tools.
What are adm-zip's main functionalities?
Creating ZIP archives
This feature allows you to create a new ZIP archive and add files to it from the local filesystem. The 'writeZip' method is then used to write the archive to disk.
const AdmZip = require('adm-zip');
const zip = new AdmZip();
zip.addLocalFile('/path/to/file.txt');
zip.writeZip('/path/to/archive.zip');
Extracting ZIP archives
This feature enables you to extract the contents of a ZIP archive to a specified directory on the filesystem. The second parameter of 'extractAllTo' determines whether to overwrite existing files.
const AdmZip = require('adm-zip');
const zip = new AdmZip('/path/to/archive.zip');
zip.extractAllTo('/path/to/extract/', true);
Reading ZIP archives
This feature is used to read the contents of a ZIP archive and list its entries. You can iterate over the entries to get information about each file or directory in the archive.
const AdmZip = require('adm-zip');
const zip = new AdmZip('/path/to/archive.zip');
const zipEntries = zip.getEntries();
zipEntries.forEach((zipEntry) => {
console.log(zipEntry.toString()); // outputs zip entries information
});
Updating ZIP archives
This feature allows you to update the contents of an existing file within a ZIP archive. The 'updateFile' method takes the filename and the new content as a Buffer.
const AdmZip = require('adm-zip');
const zip = new AdmZip('/path/to/archive.zip');
zip.updateFile('fileInsideZip.txt', Buffer.from('new content'));
zip.writeZip();
Other packages similar to adm-zip
jszip
JSZip is a popular npm package with similar functionality to adm-zip. It allows for creating, reading, and editing .zip files with JavaScript. JSZip has a more modern API and supports Promises, which can make it easier to use in asynchronous code compared to adm-zip.
archiver
Archiver is another npm package that can create archives in zip and tar formats. It provides streaming support, which can be beneficial for working with large files or for network transfers. Archiver is often preferred for its performance and flexibility.
extract-zip
Extract-zip is focused solely on extracting ZIP files. It's a simpler alternative to adm-zip if you only need to extract archives and not create or manipulate them. It uses a callback-based API for handling extraction completion.
yauzl
Yauzl is a minimalistic npm package for reading ZIP archives. It's designed to be low-level and fast, following the philosophy of 'do one thing and do it well.' Unlike adm-zip, yauzl does not provide the ability to create or write to ZIP files.
ADM-ZIP for NodeJS
ADM-ZIP is a pure JavaScript implementation for zip data compression for NodeJS.
Installation
With npm do:
$ npm install adm-zip
What is it good for?
The library allows you to:
- decompress zip files directly to disk or in memory buffers
- compress files and store them to disk in .zip format or in compressed buffers
- update content of/add new/delete files from an existing .zip
Dependencies
There are no other nodeJS libraries that ADM-ZIP is dependent of
Examples
Basic usage
var AdmZip = require('adm-zip');
var zip = new AdmZip("./my_file.zip");
var zipEntries = zip.getEntries();
zipEntries.forEach(function(zipEntry) {
console.log(zipEntry.toString());
if (zipEntry.entryName == "my_file.txt") {
console.log(zipEntry.data.toString('utf8'));
}
});
console.log(zip.readAsText("some_folder/my_file.txt"));
zip.extractEntryTo("some_folder/my_file.txt", "/home/me/tempfolder", true)
zip.extractAllTo("/home/me/zipcontent/", true);
var zip = new AdmZip();
zip.addFile("test.txt", new Buffer("inner content of the file"), "entry comment goes here");
zip.addLocalFile("/home/me/some_picture.png");
var willSendthis = zip.toBuffer();
zip.writeZip("/home/me/files.zip");
For more detailed information please check out the wiki.