Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing multiple files using zlib

Tags:

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); 
like image 934
user3180402 Avatar asked Feb 24 '14 07:02

user3180402


People also ask

Can zlib decompress Gzip?

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.

How does zlib compression work?

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.

Does Gzip use zlib?

gzip() method is an inbuilt application programming interface of the Zlib module which is used to compresses a chunk of data.


2 Answers

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:

  • https://github.com/mafintosh/tar-stream

Also see this answer for more information: Node.js - Zip/Unzip a folder

like image 179
B T Avatar answered Sep 21 '22 07:09

B T


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(); 
like image 26
Overdrivr Avatar answered Sep 19 '22 07:09

Overdrivr