The below code will compress one file. How can I compress multiple files
var gzip = zlib.createGzip(); var fs = require('fs'); var inp = fs.createReadStream('input.txt'); var out = fs.createWriteStream('input.txt.gz'); inp.pipe(gzip).pipe(out);
This package provides a pure interface for compressing and decompressing streams of data represented as lazy ByteString s. It uses the zlib C library so it has high performance. It supports the zlib , gzip and raw compression formats.
zlib compressed data are typically written with a gzip or a zlib wrapper. The wrapper encapsulates the raw DEFLATE data by adding a header and trailer. This provides stream identification and error detection that are not provided by the raw DEFLATE data.
gzip() method is an inbuilt application programming interface of the Zlib module which is used to compresses a chunk of data.
Gzip is an algorithm that compresses a string of data. It knows nothing about files or folders and so can't do what you want by itself. What you can do is use an archiver tool to build a single archive file, and then use gzip to compress the data that makes up the archive:
Also see this answer for more information: Node.js - Zip/Unzip a folder
The best package for this (and the only one still maintained and properly documented) seems to be archiver
var fs = require('fs'); var archiver = require('archiver'); var output = fs.createWriteStream('./example.tar.gz'); var archive = archiver('tar', { gzip: true, zlib: { level: 9 } // Sets the compression level. }); archive.on('error', function(err) { throw err; }); // pipe archive data to the output file archive.pipe(output); // append files archive.file('/path/to/file0.txt', {name: 'file0-or-change-this-whatever.txt'}); archive.file('/path/to/README.md', {name: 'foobar.md'}); // Wait for streams to complete archive.finalize();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With