Zlib

  1. Stability: 3 - Stable

You can access this module with:

  1. var zlib = require('zlib');

This provides bindings to Gzip/Gunzip, Deflate/Inflate, and
DeflateRaw/InflateRaw classes. Each class takes the same options, and
is a readable/writable Stream.

Examples

Compressing or decompressing a file can be done by piping an
fs.ReadStream into a zlib stream, then into an fs.WriteStream.

  1. var gzip = zlib.createGzip();
  2. var fs = require('fs');
  3. var inp = fs.createReadStream('input.txt');
  4. var out = fs.createWriteStream('input.txt.gz');
  5. inp.pipe(gzip).pipe(out);

Compressing or decompressing data in one step can be done by using
the convenience methods.

  1. var input = '.................................';
  2. zlib.deflate(input, function(err, buffer) {
  3. if (!err) {
  4. console.log(buffer.toString('base64'));
  5. }
  6. });
  7. var buffer = new Buffer('eJzT0yMAAGTvBe8=', 'base64');
  8. zlib.unzip(buffer, function(err, buffer) {
  9. if (!err) {
  10. console.log(buffer.toString());
  11. }
  12. });

To use this module in an HTTP client or server, use the
accept-encoding
on requests, and the
content-encoding
header on responses.

Note: these examples are drastically simplified to show
the basic concept.
Zlib encoding can be expensive, and the results
ought to be cached. See Memory Usage Tuning
below for more information on the speed/memory/compression
tradeoffs involved in zlib usage.

  1. // client request example
  2. var zlib = require('zlib');
  3. var http = require('http');
  4. var fs = require('fs');
  5. var request = http.get({ host: 'izs.me',
  6. path: '/',
  7. port: 80,
  8. headers: { 'accept-encoding': 'gzip,deflate' } });
  9. request.on('response', function(response) {
  10. var output = fs.createWriteStream('izs.me_index.html');
  11. switch (response.headers['content-encoding']) {
  12. // or, just use zlib.createUnzip() to handle both cases
  13. case 'gzip':
  14. response.pipe(zlib.createGunzip()).pipe(output);
  15. break;
  16. case 'deflate':
  17. response.pipe(zlib.createInflate()).pipe(output);
  18. break;
  19. default:
  20. response.pipe(output);
  21. break;
  22. }
  23. });
  24. // server example
  25. // Running a gzip operation on every request is quite expensive.
  26. // It would be much more efficient to cache the compressed buffer.
  27. var zlib = require('zlib');
  28. var http = require('http');
  29. var fs = require('fs');
  30. http.createServer(function(request, response) {
  31. var raw = fs.createReadStream('index.html');
  32. var acceptEncoding = request.headers['accept-encoding'];
  33. if (!acceptEncoding) {
  34. acceptEncoding = '';
  35. }
  36. // Note: this is not a conformant accept-encoding parser.
  37. // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
  38. if (acceptEncoding.match(/\bdeflate\b/)) {
  39. response.writeHead(200, { 'content-encoding': 'deflate' });
  40. raw.pipe(zlib.createDeflate()).pipe(response);
  41. } else if (acceptEncoding.match(/\bgzip\b/)) {
  42. response.writeHead(200, { 'content-encoding': 'gzip' });
  43. raw.pipe(zlib.createGzip()).pipe(response);
  44. } else {
  45. response.writeHead(200, {});
  46. raw.pipe(response);
  47. }
  48. }).listen(1337);

zlib.createGzip([options])

Returns a new Gzip object with an
options.

zlib.createGunzip([options])

Returns a new Gunzip object with an
options.

zlib.createDeflate([options])

Returns a new Deflate object with an
options.

zlib.createInflate([options])

Returns a new Inflate object with an
options.

zlib.createDeflateRaw([options])

Returns a new DeflateRaw object with an
options.

zlib.createInflateRaw([options])

Returns a new InflateRaw object with an
options.

zlib.createUnzip([options])

Returns a new Unzip object with an
options.

Class: zlib.Zlib

Not exported by the zlib module. It is documented here because it is the base
class of the compressor/decompressor classes.

zlib.flush([kind], callback)

kind defaults to zlib.Z_FULL_FLUSH.

Flush pending data. Don’t call this frivolously, premature flushes negatively
impact the effectiveness of the compression algorithm.

zlib.params(level, strategy, callback)

Dynamically update the compression level and compression strategy.
Only applicable to deflate algorithm.

zlib.reset()

Reset the compressor/decompressor to factory defaults. Only applicable to
the inflate and deflate algorithms.

Class: zlib.Gzip

Compress data using gzip.

Class: zlib.Gunzip

Decompress a gzip stream.

Class: zlib.Deflate

Compress data using deflate.

Class: zlib.Inflate

Decompress a deflate stream.

Class: zlib.DeflateRaw

Compress data using deflate, and do not append a zlib header.

Class: zlib.InflateRaw

Decompress a raw deflate stream.

Class: zlib.Unzip

Decompress either a Gzip- or Deflate-compressed stream by auto-detecting
the header.

Convenience Methods

All of these take a string or buffer as the first argument, an optional second
argument to supply options to the zlib classes and will call the supplied
callback with callback(error, result).

Every method has a *Sync counterpart, which accept the same arguments, but
without a callback.

zlib.deflate(buf[, options], callback)

zlib.deflateSync(buf[, options])

Compress a string with Deflate.

zlib.deflateRaw(buf[, options], callback)

zlib.deflateRawSync(buf[, options])

Compress a string with DeflateRaw.

zlib.gzip(buf[, options], callback)

zlib.gzipSync(buf[, options])

Compress a string with Gzip.

zlib.gunzip(buf[, options], callback)

zlib.gunzipSync(buf[, options])

Decompress a raw Buffer with Gunzip.

zlib.inflate(buf[, options], callback)

zlib.inflateSync(buf[, options])

Decompress a raw Buffer with Inflate.

zlib.inflateRaw(buf[, options], callback)

zlib.inflateRawSync(buf[, options])

Decompress a raw Buffer with InflateRaw.

zlib.unzip(buf[, options], callback)

zlib.unzipSync(buf[, options])

Decompress a raw Buffer with Unzip.

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.

  • flush (default: zlib.Z_NO_FLUSH)
  • chunkSize (default: 16*1024)
  • windowBits
  • level (compression only)
  • memLevel (compression only)
  • strategy (compression only)
  • dictionary (deflate/inflate only, empty dictionary by default)

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. (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:

  1. { 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. 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.

The speed of zlib compression is affected most dramatically by the
level setting. A higher level will result in better compression, but
will take longer to complete. A lower level will result in less
compression, but will be much faster.

In general, greater memory usage options will mean that node has to make
fewer calls to zlib, since it’ll be able to process more data in a
single write operation. So, this is another factor that affects the
speed, at the cost of memory usage.

Constants

All of the constants defined in zlib.h are also defined on
require('zlib').
In the normal course of operations, you will not need to ever set any of
these. They are documented here so that their presence is not
surprising. This section is taken almost directly from the zlib
documentation
. See
http://zlib.net/manual.html#Constants for more details.

Allowed flush values.

  • zlib.Z_NO_FLUSH
  • zlib.Z_PARTIAL_FLUSH
  • zlib.Z_SYNC_FLUSH
  • zlib.Z_FULL_FLUSH
  • zlib.Z_FINISH
  • zlib.Z_BLOCK
  • zlib.Z_TREES

Return codes for the compression/decompression functions. Negative
values are errors, positive values are used for special but normal
events.

  • zlib.Z_OK
  • zlib.Z_STREAM_END
  • zlib.Z_NEED_DICT
  • zlib.Z_ERRNO
  • zlib.Z_STREAM_ERROR
  • zlib.Z_DATA_ERROR
  • zlib.Z_MEM_ERROR
  • zlib.Z_BUF_ERROR
  • zlib.Z_VERSION_ERROR

Compression levels.

  • zlib.Z_NO_COMPRESSION
  • zlib.Z_BEST_SPEED
  • zlib.Z_BEST_COMPRESSION
  • zlib.Z_DEFAULT_COMPRESSION

Compression strategy.

  • zlib.Z_FILTERED
  • zlib.Z_HUFFMAN_ONLY
  • zlib.Z_RLE
  • zlib.Z_FIXED
  • zlib.Z_DEFAULT_STRATEGY

Possible values of the data_type field.

  • zlib.Z_BINARY
  • zlib.Z_TEXT
  • zlib.Z_ASCII
  • zlib.Z_UNKNOWN

The deflate compression method (the only one supported in this version).

  • zlib.Z_DEFLATED

For initializing zalloc, zfree, opaque.

  • zlib.Z_NULL