What is zlib? How do you compress a file in Node.js?
The zlib
module provides compression functionality implemented using Gzip and Deflate/Inflate.
var zlib = require('zlib');
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);
Calling .flush()
on a compression stream will make zlib
return as much output as currently possible. This may come at the cost of degraded compression quality, but can be useful when data needs to be available as soon as possible.
Comments
Post a Comment