How To Work With Zip Files in Node.js

Working with zip files in Node.js involves using third-party libraries as Node.js doesn't provide built-in support for zip file manipulation. One of the popular libraries for handling zip files in Node.js is adm-zip. Below are the steps to work with zip files using adm-zip:

  1. Install adm-zip:

    You can install adm-zip using npm:

                    
                        npm install adm-zip
                    
                

  2. Extracting Files from a Zip Archive:

                    
                        const AdmZip = require('adm-zip');
    
                        // Load a zip file
                        const zip = new AdmZip('example.zip');
                        
                        // Extract all files to the specified directory
                        zip.extractAllTo(/* target path */, /* overwrite */ true);                    
                    
                

  3. Adding Files to a Zip Archive:

                    
                        const AdmZip = require('adm-zip');
    
                        // Create a new zip file
                        const zip = new AdmZip();
                        
                        // Add a file
                        zip.addFile(/* file name */, /* Buffer or content */, /* comment */);
                        
                        // Save the zip file
                        zip.writeZip(/* target path */);                    
                    
                

  4. Reading Contents of a Zip Archive:

                    
                        const AdmZip = require('adm-zip');
    
                        // Load a zip file
                        const zip = new AdmZip('example.zip');
                        
                        // Get an array of zip entries
                        const zipEntries = zip.getEntries();
                        
                        // Display the contents
                        zipEntries.forEach(zipEntry => {
                            console.log(zipEntry.entryName);
                        });                    
                    
                

  5. Extracting a Specific File from a Zip Archive:

                    
                        const AdmZip = require('adm-zip');
    
                        // Load a zip file
                        const zip = new AdmZip('example.zip');
                        
                        // Extract a specific file
                        const zipEntry = zip.getEntry(/* file name */);
                        if (zipEntry) {
                            zip.extractEntryTo(zipEntry, /* target path */, /* maintainEntryPath */ true, /* overwrite */ true);
                        }                    
                    
                

    Example:

    Extracting all files from a zip archive:

                    
                        const AdmZip = require('adm-zip');
    
                        const zip = new AdmZip('example.zip');
                        zip.extractAllTo('extracted_files', true);                    
                    
                

    Adding files to a zip archive:

                    
                        const AdmZip = require('adm-zip');
                        const fs = require('fs');
                        
                        const zip = new AdmZip();
                        const buffer = fs.readFileSync('file.txt');
                        zip.addFile('file.txt', buffer, 'This is a comment');
                        zip.writeZip('example.zip');                    
                    
                

    Reading contents of a zip archive:

                    
                        const AdmZip = require('adm-zip');
    
                        const zip = new AdmZip('example.zip');
                        const zipEntries = zip.getEntries();
                        zipEntries.forEach(zipEntry => {
                            console.log(zipEntry.entryName);
                        });                    
                    
                

With adm-zip, you can easily manipulate zip files in Node.js, including extracting, adding, and reading files from zip archives.

How To Install and Use the Yarn Package Manager for Node.js

This command will guide you through creating a package.json file for your project, where you can specify project details and dependencies. This command installs all dependencies listed in package.json into a node_modules folder in your project direct …

read more

What is Object-Oriented Design? How Many Principles of Object Oriented Desi …

Object-Oriented Design (OOD) is a methodology for designing software systems based on the concept of "objects," which are instances of classes that encapsulate data and behavior. OOD aims to create modular, reusable, and maintainable softwa …

read more