As a developer, one frequently encounters various file formats that have to be dealt with efficiently. One such format is the zip file format, which is commonly utilized to package multiple files and folders as a single entity. A major advantage of the ZIP file format is its compression capability that results in small file size. A well-known tool for manipulating ZIP files in JavaScript is the adm-zip library which we will be exploring in this article.
First, let's introduce the npm package adm-zip. adm-zip is an open-source library available on npm that provides a simple way to read, extract and update Zip files in a Node.js environment. The adm-zip library has been widely embraced by the Node.js community due to its efficiency and ease of use. Its popularity is evident from the fact that it has been downloaded over three and a half million times as of September 2021.
In this article, we will be discussing adm-zip, its installation and how to use it to extract files from a ZIP archive, add files to an existing archive, and create a new archive. We will be covering the basic features of adm-zip to help you get started. This article assumes that you have a basic understanding of Node.js and its module system, npm.
Installation:
As with any other npm package, adm-zip can be installed as follows:
npm install adm-zip
Once installed, an application can now create an instance of adm-zip as follows:
const AdmZip = require('adm-zip');
Extract Files from a ZIP Archive:
To extract files from a ZIP archive, we first need to create an instance of the adm-zip class and load the ZIP file. We can then perform tasks such as extracting files, listing the archived files, getting the number of entries and checking if a file or folder exists within the archive. Below is an example code snippet that demonstrates how to extract all files from a ZIP archive using adm-zip.
const zip = new AdmZip('./path/to/archive.zip');
const zipEntries = zip.getEntries(); // Get the archive entries object
zipEntries.forEach((entry) => {
if (entry.entryName.includes('.txt')) {
const buffer = zip.readFile(entry.entryName);
// Write the content of the file to a new file
fs.writeFileSync(`./${entry.entryName}`, buffer);
}
});
Create a New ZIP Archive:
To create a new ZIP archive, we first need to create an instance of the adm-zip class and then populate it with files. We can then save the new archive to disk or get the archive buffer. Below is an example code snippet that demonstrates how to create a new ZIP archive and add files to it using adm-zip.
const zip = new AdmZip();
zip.addLocalFile('./path/to/myfile.txt'); // Add file to the archive with path
zip.addFile(`${Date.now()}.txt`, Buffer.from('Hello, world!')); // Add file with content
zip.writeZip('./mynewarchive.zip'); // Save the new archive to disk
Update an Existing ZIP Archive:
To update an existing archive, we first need to create an instance of the adm-zip class and load the archive. We can then add, remove or replace archived files or update the archive comment. Below is an example code snippet that demonstrates how to update an existing ZIP archive using adm-zip.
const zip = new AdmZip('./path/to/archive.zip');
zip.addLocalFile('./newfile.txt'); // Add a new file to the archive
zip.deleteFile('oldfile.txt'); // Remove old file from the archive
zip.updateFile('existingfile.txt', Buffer.from('New Content')); // Update existing file
zip.writeZip('./updatedarchive.zip'); // Save the updated archive to disk
Conclusion:
In conclusion, the adm-zip package is a reliable and efficient way to manipulate ZIP archives in your Node.js applications. In this article, we covered the basics of adm-zip and its installation, as well as how to extract files, create a new archive, and update an existing archive using adm-zip library. This package is definitely worth considering for your next Node.js project. With this knowledge, you can now better handle ZIP files in your Node.js applications efficiently.
let's dive deeper into some of the topics covered in the article.
Extracting Files from a ZIP Archive:
When using adm-zip to extract files from a ZIP archive, we first create an instance of the AdmZip class and load the archive using the path of the archive file. Once we have loaded the archive, we use the getEntries() method to get an array of all the entries in the archive. Each entry in the array is an object that contains information about the file, such as its name, size, and modification date, among other attributes.
To extract a file from the archive, we can use the readFile() method of the AdmZip class to read the contents of the file and store it in a buffer. We can then write this buffer to a new file using the Node.js fs (file system) module. In the example provided in the article, we filter out only the text files in the archive and write their contents to new files. However, you can modify this code to extract any type of file from the archive and store it in any location of your choice.
Creating a New ZIP Archive:
To create a new ZIP archive using adm-zip, we first create an instance of the AdmZip class with no arguments. We can then add files to the archive using the addLocalFile() method or the addFile() method. The addLocalFile() method is used to add a file from the local file system to the archive by specifying the path to the file, while the addFile() method is used to add a file to the archive by specifying its name and contents in the form of a buffer.
After adding files to the archive, we can call the writeZip() method to save the archive to a file on the local file system. Alternatively, we can use the toBuffer() method to get the archive buffer and work with it in memory. Keep in mind that creating a new archive using adm-zip will overwrite any existing archive with the same name at the specified location.
Updating an Existing ZIP Archive:
To update an existing ZIP archive using adm-zip, we first need to create an instance of the AdmZip class and load the existing archive using the path to the archive file. Once the archive is loaded, we can use various methods such as addLocalFile() to add a new file to the archive, deleteFile() to remove a file from the archive, and updateFile() to update the contents of an existing file.
After making changes to the archive, we can save them to a new archive using the writeZip() method. Note that this will overwrite any existing archive with the same name at the specified location, so it is a good idea to use a different name or location for the updated archive.
In summary, adm-zip provides a simple and efficient way to manipulate ZIP archives in Node.js applications. With its easy-to-use methods for extracting, creating, and updating archives, you can work with ZIP files in your projects with ease.
Popular questions
- What is adm-zip?
Adm-zip is an open-source npm package that provides an easy way to read, extract, and update Zip files in a Node.js environment.
- How do you extract files from a ZIP archive using adm-zip?
To extract files from a ZIP archive using adm-zip, we first create an instance of the AdmZip class and load the archive using the archive file path. We can then use the getEntries() method to get an array of all the entries in the archive. To extract a file, we can use the readFile() method to read the contents of the file and store it in a buffer. We can then write this buffer to a new file using the Node.js fs module.
- How do you create a new ZIP archive using adm-zip?
To create a new ZIP archive using adm-zip, we first create an instance of the AdmZip class with no arguments. We can then use the addLocalFile() method or the addFile() method to add files to the archive. The addLocalFile() method is used to add a file from the local file system to the archive, while the addFile() method is used to add a file to the archive by specifying its name and contents in the form of a buffer. After adding files to the archive, we can call the writeZip() method to save the archive to a file on the local file system.
- How do you update an existing ZIP archive using adm-zip?
To update an existing ZIP archive using adm-zip, we first create an instance of the AdmZip class and load the archive using the archive file path. We can then use methods such as addLocalFile() to add a new file to the archive, deleteFile() to remove a file from the archive, and updateFile() to update the contents of an existing file. After making changes to the archive, we can save them to a new archive using the writeZip() method.
- How does adm-zip compare to other ZIP libraries in Node.js?
Adm-zip is a popular npm package used to manipulate ZIP archives in Node.js. Its popularity is due to its ease of use and efficiency. Some other ZIP libraries in Node.js include node-stream-zip and yauzl. Node-stream-zip is another popular package that provides streaming support for ZIP archives, while yauzl is a promise-based ZIP library that offers similar functionality to adm-zip.
Tag
Archiving