{ "source": "doc/api/zlib.markdown", "modules": [ { "textRaw": "Zlib", "name": "zlib", "stability": 3, "stabilityText": "Stable", "desc": "
You can access this module with:\n\n
\nvar zlib = require('zlib');
\nThis provides bindings to Gzip/Gunzip, Deflate/Inflate, and\nDeflateRaw/InflateRaw classes. Each class takes the same options, and\nis a readable/writable Stream.\n\n
\nCompressing or decompressing a file can be done by piping an\nfs.ReadStream into a zlib stream, then into an fs.WriteStream.\n\n
\nvar gzip = zlib.createGzip();\nvar fs = require('fs');\nvar inp = fs.createReadStream('input.txt');\nvar out = fs.createWriteStream('input.txt.gz');\n\ninp.pipe(gzip).pipe(out);
\nCompressing or decompressing data in one step can be done by using\nthe convenience methods.\n\n
\nvar input = '.................................';\nzlib.deflate(input, function(err, buffer) {\n if (!err) {\n console.log(buffer.toString('base64'));\n }\n});\n\nvar buffer = new Buffer('eJzT0yMAAGTvBe8=', 'base64');\nzlib.unzip(buffer, function(err, buffer) {\n if (!err) {\n console.log(buffer.toString());\n }\n});
\nTo use this module in an HTTP client or server, use the\naccept-encoding\non requests, and the\ncontent-encoding\nheader on responses.\n\n
\nNote: these examples are drastically simplified to show\nthe basic concept. Zlib encoding can be expensive, and the results\nought to be cached. See Memory Usage Tuning\nbelow for more information on the speed/memory/compression\ntradeoffs involved in zlib usage.\n\n
\n// client request example\nvar zlib = require('zlib');\nvar http = require('http');\nvar fs = require('fs');\nvar request = http.get({ host: 'izs.me',\n path: '/',\n port: 80,\n headers: { 'accept-encoding': 'gzip,deflate' } });\nrequest.on('response', function(response) {\n var output = fs.createWriteStream('izs.me_index.html');\n\n switch (response.headers['content-encoding']) {\n // or, just use zlib.createUnzip() to handle both cases\n case 'gzip':\n response.pipe(zlib.createGunzip()).pipe(output);\n break;\n case 'deflate':\n response.pipe(zlib.createInflate()).pipe(output);\n break;\n default:\n response.pipe(output);\n break;\n }\n});\n\n// server example\n// Running a gzip operation on every request is quite expensive.\n// It would be much more efficient to cache the compressed buffer.\nvar zlib = require('zlib');\nvar http = require('http');\nvar fs = require('fs');\nhttp.createServer(function(request, response) {\n var raw = fs.createReadStream('index.html');\n var acceptEncoding = request.headers['accept-encoding'];\n if (!acceptEncoding) {\n acceptEncoding = '';\n }\n\n // Note: this is not a conformant accept-encoding parser.\n // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3\n if (acceptEncoding.match(/\\bdeflate\\b/)) {\n response.writeHead(200, { 'content-encoding': 'deflate' });\n raw.pipe(zlib.createDeflate()).pipe(response);\n } else if (acceptEncoding.match(/\\bgzip\\b/)) {\n response.writeHead(200, { 'content-encoding': 'gzip' });\n raw.pipe(zlib.createGzip()).pipe(response);\n } else {\n response.writeHead(200, {});\n raw.pipe(response);\n }\n}).listen(1337);
\n",
"methods": [
{
"textRaw": "zlib.createGzip([options])",
"type": "method",
"name": "createGzip",
"desc": "Returns a new Gzip object with an\noptions.\n\n
\n", "signatures": [ { "params": [ { "name": "options", "optional": true } ] } ] }, { "textRaw": "zlib.createGunzip([options])", "type": "method", "name": "createGunzip", "desc": "Returns a new Gunzip object with an\noptions.\n\n
\n", "signatures": [ { "params": [ { "name": "options", "optional": true } ] } ] }, { "textRaw": "zlib.createDeflate([options])", "type": "method", "name": "createDeflate", "desc": "Returns a new Deflate object with an\noptions.\n\n
\n", "signatures": [ { "params": [ { "name": "options", "optional": true } ] } ] }, { "textRaw": "zlib.createInflate([options])", "type": "method", "name": "createInflate", "desc": "Returns a new Inflate object with an\noptions.\n\n
\n", "signatures": [ { "params": [ { "name": "options", "optional": true } ] } ] }, { "textRaw": "zlib.createDeflateRaw([options])", "type": "method", "name": "createDeflateRaw", "desc": "Returns a new DeflateRaw object with an\noptions.\n\n
\n", "signatures": [ { "params": [ { "name": "options", "optional": true } ] } ] }, { "textRaw": "zlib.createInflateRaw([options])", "type": "method", "name": "createInflateRaw", "desc": "Returns a new InflateRaw object with an\noptions.\n\n
\n", "signatures": [ { "params": [ { "name": "options", "optional": true } ] } ] }, { "textRaw": "zlib.createUnzip([options])", "type": "method", "name": "createUnzip", "desc": "Returns a new Unzip object with an\noptions.\n\n\n
\n", "signatures": [ { "params": [ { "name": "options", "optional": true } ] } ] }, { "textRaw": "zlib.deflate(buf, callback)", "type": "method", "name": "deflate", "desc": "Compress a string with Deflate.\n\n
\n", "signatures": [ { "params": [ { "name": "buf" }, { "name": "callback" } ] } ] }, { "textRaw": "zlib.deflateRaw(buf, callback)", "type": "method", "name": "deflateRaw", "desc": "Compress a string with DeflateRaw.\n\n
\n", "signatures": [ { "params": [ { "name": "buf" }, { "name": "callback" } ] } ] }, { "textRaw": "zlib.gzip(buf, callback)", "type": "method", "name": "gzip", "desc": "Compress a string with Gzip.\n\n
\n", "signatures": [ { "params": [ { "name": "buf" }, { "name": "callback" } ] } ] }, { "textRaw": "zlib.gunzip(buf, callback)", "type": "method", "name": "gunzip", "desc": "Decompress a raw Buffer with Gunzip.\n\n
\n", "signatures": [ { "params": [ { "name": "buf" }, { "name": "callback" } ] } ] }, { "textRaw": "zlib.inflate(buf, callback)", "type": "method", "name": "inflate", "desc": "Decompress a raw Buffer with Inflate.\n\n
\n", "signatures": [ { "params": [ { "name": "buf" }, { "name": "callback" } ] } ] }, { "textRaw": "zlib.inflateRaw(buf, callback)", "type": "method", "name": "inflateRaw", "desc": "Decompress a raw Buffer with InflateRaw.\n\n
\n", "signatures": [ { "params": [ { "name": "buf" }, { "name": "callback" } ] } ] }, { "textRaw": "zlib.unzip(buf, callback)", "type": "method", "name": "unzip", "desc": "Decompress a raw Buffer with Unzip.\n\n
\n", "signatures": [ { "params": [ { "name": "buf" }, { "name": "callback" } ] } ] } ], "classes": [ { "textRaw": "Class: zlib.Gzip", "type": "class", "name": "zlib.Gzip", "desc": "Compress data using gzip.\n\n
\n" }, { "textRaw": "Class: zlib.Gunzip", "type": "class", "name": "zlib.Gunzip", "desc": "Decompress a gzip stream.\n\n
\n" }, { "textRaw": "Class: zlib.Deflate", "type": "class", "name": "zlib.Deflate", "desc": "Compress data using deflate.\n\n
\n" }, { "textRaw": "Class: zlib.Inflate", "type": "class", "name": "zlib.Inflate", "desc": "Decompress a deflate stream.\n\n
\n" }, { "textRaw": "Class: zlib.DeflateRaw", "type": "class", "name": "zlib.DeflateRaw", "desc": "Compress data using deflate, and do not append a zlib header.\n\n
\n" }, { "textRaw": "Class: zlib.InflateRaw", "type": "class", "name": "zlib.InflateRaw", "desc": "Decompress a raw deflate stream.\n\n
\n" }, { "textRaw": "Class: zlib.Unzip", "type": "class", "name": "zlib.Unzip", "desc": "Decompress either a Gzip- or Deflate-compressed stream by auto-detecting\nthe header.\n\n
\n" } ], "miscs": [ { "textRaw": "Convenience Methods", "name": "Convenience Methods", "type": "misc", "desc": "All of these take a string or buffer as the first argument, and call the\nsupplied callback with callback(error, result)
. The\ncompression/decompression engine is created using the default settings\nin all convenience methods. To supply different options, use the\nzlib classes directly.\n\n
Each class takes an options object. All options are optional. (The\nconvenience methods use the default settings for all options.)\n\n
\nNote that some options are only\nrelevant when compressing, and are ignored by the decompression classes.\n\n
\nSee the description of deflateInit2
and inflateInit2
\nat http://zlib.net/manual.html#Advanced for more information on these.\n\n
From zlib/zconf.h
, modified to node's usage:\n\n
The memory requirements for deflate are (in bytes):\n\n
\n(1 << (windowBits+2)) + (1 << (memLevel+9))
\nthat is: 128K for windowBits=15 + 128K for memLevel = 8\n(default values) plus a few kilobytes for small objects.\n\n
\nFor example, if you want to reduce\nthe default memory requirements from 256K to 128K, set the options to:\n\n
\n{ windowBits: 14, memLevel: 7 }
\nOf course this will generally degrade compression (there's no free lunch).\n\n
\nThe memory requirements for inflate are (in bytes)\n\n
\n1 << windowBits
\nthat is, 32K for windowBits=15 (default value) plus a few kilobytes\nfor small objects.\n\n
\nThis is in addition to a single internal output slab buffer of size\nchunkSize
, which defaults to 16K.\n\n
The speed of zlib compression is affected most dramatically by the\nlevel
setting. A higher level will result in better compression, but\nwill take longer to complete. A lower level will result in less\ncompression, but will be much faster.\n\n
In general, greater memory usage options will mean that node has to make\nfewer calls to zlib, since it'll be able to process more data in a\nsingle write
operation. So, this is another factor that affects the\nspeed, at the cost of memory usage.\n\n
All of the constants defined in zlib.h are also defined on\nrequire('zlib')
.\nIn the normal course of operations, you will not need to ever set any of\nthese. They are documented here so that their presence is not\nsurprising. This section is taken almost directly from the zlib\ndocumentation. See\n
http://zlib.net/manual.html#Constants for more details.\n\n
\nAllowed flush values.\n\n
\nzlib.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\nvalues are errors, positive values are used for special but normal\nevents.\n\n
\nzlib.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.\n\n
\nzlib.Z_NO_COMPRESSION
zlib.Z_BEST_SPEED
zlib.Z_BEST_COMPRESSION
zlib.Z_DEFAULT_COMPRESSION
Compression strategy.\n\n
\nzlib.Z_FILTERED
zlib.Z_HUFFMAN_ONLY
zlib.Z_RLE
zlib.Z_FIXED
zlib.Z_DEFAULT_STRATEGY
Possible values of the data_type field.\n\n
\nzlib.Z_BINARY
zlib.Z_TEXT
zlib.Z_ASCII
zlib.Z_UNKNOWN
The deflate compression method (the only one supported in this version).\n\n
\nzlib.Z_DEFLATED
For initializing zalloc, zfree, opaque.\n\n
\nzlib.Z_NULL