2.2 KiB
Zlib
You can access this module with:
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);
This provides bindings to Gzip/Gunzip, Deflate/Inflate, and DeflateRaw/InflateRaw classes. Each class takes the same options, and is a readable/writable Stream.
Constants
All of the constants defined in zlib.h are also defined on
require('zlib')
. They are described in more detail in the zlib
documentation. See http://zlib.net/manual.html#Constants
for more details.
zlib.Gzip
Compress data using gzip.
zlib.Gunzip
Decompress a gzip stream.
zlib.Deflate
Compress data using deflate.
zlib.Inflate
Decompress a deflate stream.
zlib.DeflateRaw
Compress data using deflate, and do not append a zlib header.
zlib.InflateRaw
Decompress a raw deflate stream.
zlib.Unzip
Decompress either a Gzip- or Deflate-compressed stream by auto-detecting the header.
Options
Each class takes an options object. All options are optional.
Note that some options are only relevant when compressing, and are ignored by the decompression classes.
- chunkSize (default: 16*1024)
- windowBits
- level (compression only)
- memLevel (compression only)
- strategy (compression only)
See the description of deflateInit2
and inflateInit2
at
http://zlib.net/manual.html#Advanced for more information on these.
Memory Usage Tuning
From zlib/zconf.h
, modified to node's usage:
The memory requirements for deflate are (in bytes):
(1 << (windowBits+2)) + (1 << (memLevel+9))
that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) plus a few kilobytes for small objects.
For example, if you want to reduce the default memory requirements from 256K to 128K, set the options to:
{ windowBits: 14, memLevel: 7 }
Of course this will generally degrade compression (there's no free lunch).
The memory requirements for inflate are (in bytes)
1 << windowBits
that is, 32K for windowBits=15 (default value) plus a few kilobytes for small objects.
This is in addition to a single internal output slab buffer of size
chunkSize
, which defaults to 16K.