Compress a file in Node.js


Posted on Jul 27, 2020


featured image
Compress a file in Node.js without extra dependencies.
      
   const zlib = require('zlib');
   const fs = require('fs');
   /**
     *
     * @param filePath the absolute path to the will to zip
     * @return {Promise} the full absolute path to the zip file
     */
    function zip(filePath) {
        return new Promise((resolve, reject) => {
            const zipFilePath = `${filePath}.zip`;
            pipeline(
                fs.createReadStream(filePath),
                zlib.createGzip(),
                fs.createWriteStream(zipFilePath),
                (err) => {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(zipFilePath);
                    }
                },
            );
        });
    }
      
    
Photo by JJ Ying on Unsplash
nodejs utils dev zip zlib
Search
Side Widget
You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers!