Skip to content

Latest commit

Β 

History

History
694 lines (570 loc) Β· 20.1 KB

File metadata and controls

694 lines (570 loc) Β· 20.1 KB
Β 
Feb 27, 2012
Feb 27, 2012
1
# Zlib
Sep 18, 2011
Sep 18, 2011
2
Aug 4, 2016
Aug 4, 2016
3
> Stability: 2 - Stable
Mar 4, 2012
Mar 4, 2012
4
May 16, 2016
May 16, 2016
5
The `zlib` module provides compression functionality implemented using Gzip and
6
Deflate/Inflate. It can be accessed using:
Sep 18, 2011
Sep 18, 2011
7
May 16, 2016
May 16, 2016
8
```js
9
const zlib = require('zlib');
10
```
Oct 14, 2011
Oct 14, 2011
11
Nov 18, 2016
Nov 18, 2016
12
Compressing or decompressing a stream (such as a file) can be accomplished by
May 16, 2016
May 16, 2016
13
piping the source stream data through a `zlib` stream into a destination stream:
Oct 14, 2011
Oct 14, 2011
14
Jan 21, 2016
Jan 21, 2016
15
```js
16
const gzip = zlib.createGzip();
17
const fs = require('fs');
18
const inp = fs.createReadStream('input.txt');
19
const out = fs.createWriteStream('input.txt.gz');
Sep 18, 2011
Sep 18, 2011
20
Jan 21, 2016
Jan 21, 2016
21
inp.pipe(gzip).pipe(out);
22
```
Sep 18, 2011
Sep 18, 2011
23
May 16, 2016
May 16, 2016
24
It is also possible to compress or decompress data in a single step:
Nov 4, 2011
Nov 4, 2011
25
Jan 21, 2016
Jan 21, 2016
26
```js
27
const input = '.................................';
Jan 27, 2016
Jan 27, 2016
28
zlib.deflate(input, (err, buffer) => {
Jan 21, 2016
Jan 21, 2016
29
if (!err) {
30
console.log(buffer.toString('base64'));
Jan 27, 2016
Jan 27, 2016
31
} else {
32
// handle error
Jan 21, 2016
Jan 21, 2016
33
}
34
});
35
Apr 27, 2016
Apr 27, 2016
36
const buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64');
Jan 27, 2016
Jan 27, 2016
37
zlib.unzip(buffer, (err, buffer) => {
Jan 21, 2016
Jan 21, 2016
38
if (!err) {
39
console.log(buffer.toString());
Jan 27, 2016
Jan 27, 2016
40
} else {
41
// handle error
Jan 21, 2016
Jan 21, 2016
42
}
43
});
44
```
Nov 4, 2011
Nov 4, 2011
45
May 16, 2016
May 16, 2016
46
## Compressing HTTP requests and responses
47
48
The `zlib` module can be used to implement support for the `gzip` and `deflate`
Nov 18, 2016
Nov 18, 2016
49
content-encoding mechanisms defined by
May 16, 2016
May 16, 2016
50
[HTTP](https://tools.ietf.org/html/rfc7230#section-4.2).
51
52
The HTTP [`Accept-Encoding`][] header is used within an http request to identify
Nov 18, 2016
Nov 18, 2016
53
the compression encodings accepted by the client. The [`Content-Encoding`][]
54
header is used to identify the compression encodings actually applied to a
May 16, 2016
May 16, 2016
55
message.
Oct 14, 2011
Oct 14, 2011
56
May 25, 2017
May 25, 2017
57
*Note*: the examples given below are drastically simplified to show
58
the basic concept. Using `zlib` encoding can be expensive, and the results
Jan 11, 2016
Jan 11, 2016
59
ought to be cached. See [Memory Usage Tuning][] for more information
May 16, 2016
May 16, 2016
60
on the speed/memory/compression tradeoffs involved in `zlib` usage.
Oct 14, 2011
Oct 14, 2011
61
Jan 21, 2016
Jan 21, 2016
62
```js
63
// client request example
64
const zlib = require('zlib');
65
const http = require('http');
66
const fs = require('fs');
May 16, 2016
May 16, 2016
67
const request = http.get({ host: 'example.com',
Apr 24, 2017
Apr 24, 2017
68
path: '/',
69
port: 80,
70
headers: { 'Accept-Encoding': 'gzip,deflate' } });
Jan 21, 2016
Jan 21, 2016
71
request.on('response', (response) => {
Apr 24, 2017
Apr 24, 2017
72
const output = fs.createWriteStream('example.com_index.html');
Jan 21, 2016
Jan 21, 2016
73
74
switch (response.headers['content-encoding']) {
75
// or, just use zlib.createUnzip() to handle both cases
76
case 'gzip':
77
response.pipe(zlib.createGunzip()).pipe(output);
78
break;
79
case 'deflate':
80
response.pipe(zlib.createInflate()).pipe(output);
81
break;
82
default:
83
response.pipe(output);
84
break;
85
}
86
});
Apr 24, 2017
Apr 24, 2017
87
```
Jan 21, 2016
Jan 21, 2016
88
Apr 24, 2017
Apr 24, 2017
89
```js
Jan 21, 2016
Jan 21, 2016
90
// server example
91
// Running a gzip operation on every request is quite expensive.
92
// It would be much more efficient to cache the compressed buffer.
93
const zlib = require('zlib');
94
const http = require('http');
95
const fs = require('fs');
96
http.createServer((request, response) => {
Apr 24, 2017
Apr 24, 2017
97
const raw = fs.createReadStream('index.html');
98
let acceptEncoding = request.headers['accept-encoding'];
Jan 21, 2016
Jan 21, 2016
99
if (!acceptEncoding) {
100
acceptEncoding = '';
101
}
102
May 25, 2017
May 25, 2017
103
// Note: This is not a conformant accept-encoding parser.
Jan 21, 2016
Jan 21, 2016
104
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
105
if (acceptEncoding.match(/\bdeflate\b/)) {
May 16, 2016
May 16, 2016
106
response.writeHead(200, { 'Content-Encoding': 'deflate' });
Jan 21, 2016
Jan 21, 2016
107
raw.pipe(zlib.createDeflate()).pipe(response);
108
} else if (acceptEncoding.match(/\bgzip\b/)) {
May 16, 2016
May 16, 2016
109
response.writeHead(200, { 'Content-Encoding': 'gzip' });
Jan 21, 2016
Jan 21, 2016
110
raw.pipe(zlib.createGzip()).pipe(response);
111
} else {
112
response.writeHead(200, {});
113
raw.pipe(response);
114
}
115
}).listen(1337);
116
```
Sep 18, 2011
Sep 18, 2011
117
Oct 17, 2016
Oct 17, 2016
118
By default, the `zlib` methods will throw an error when decompressing
Apr 17, 2016
Apr 17, 2016
119
truncated data. However, if it is known that the data is incomplete, or
120
the desire is to inspect only the beginning of a compressed file, it is
121
possible to suppress the default error handling by changing the flushing
122
method that is used to compressed the last chunk of input data:
123
124
```js
125
// This is a truncated version of the buffer from the above examples
Apr 27, 2016
Apr 27, 2016
126
const buffer = Buffer.from('eJzT0yMA', 'base64');
Apr 17, 2016
Apr 17, 2016
127
Apr 24, 2017
Apr 24, 2017
128
zlib.unzip(
129
buffer,
130
{finishFlush: zlib.constants.Z_SYNC_FLUSH},
131
(err, buffer) => {
132
if (!err) {
133
console.log(buffer.toString());
134
} else {
135
// handle error
136
}
137
});
Apr 17, 2016
Apr 17, 2016
138
```
139
140
This will not change the behavior in other error-throwing situations, e.g.
141
when the input data has an invalid format. Using this method, it will not be
142
possible to determine whether the input ended prematurely or lacks the
143
integrity checks, making it necessary to manually check that the
144
decompressed result is valid.
145
Feb 27, 2012
Feb 27, 2012
146
## Memory Usage Tuning
147
148
<!--type=misc-->
Sep 18, 2011
Sep 18, 2011
149
Aug 23, 2015
Aug 23, 2015
150
From `zlib/zconf.h`, modified to node.js's usage:
Sep 18, 2011
Sep 18, 2011
151
152
The memory requirements for deflate are (in bytes):
153
Apr 24, 2017
Apr 24, 2017
154
<!-- eslint-disable semi -->
Jul 14, 2016
Jul 14, 2016
155
```js
Apr 24, 2017
Apr 24, 2017
156
(1 << (windowBits + 2)) + (1 << (memLevel + 9))
Jan 21, 2016
Jan 21, 2016
157
```
Sep 18, 2011
Sep 18, 2011
158
May 16, 2016
May 16, 2016
159
That is: 128K for windowBits=15 + 128K for memLevel = 8
Sep 18, 2011
Sep 18, 2011
160
(default values) plus a few kilobytes for small objects.
161
May 16, 2016
May 16, 2016
162
For example, to reduce the default memory requirements from 256K to 128K, the
Sep 2, 2016
Sep 2, 2016
163
options should be set to:
Sep 18, 2011
Sep 18, 2011
164
Jul 14, 2016
Jul 14, 2016
165
```js
Apr 24, 2017
Apr 24, 2017
166
const options = { windowBits: 14, memLevel: 7 };
Jan 21, 2016
Jan 21, 2016
167
```
Sep 18, 2011
Sep 18, 2011
168
May 16, 2016
May 16, 2016
169
This will, however, generally degrade compression.
Sep 18, 2011
Sep 18, 2011
170
Apr 24, 2017
Apr 24, 2017
171
The memory requirements for inflate are (in bytes) `1 << windowBits`.
May 16, 2016
May 16, 2016
172
That is, 32K for windowBits=15 (default value) plus a few kilobytes
Sep 18, 2011
Sep 18, 2011
173
for small objects.
174
175
This is in addition to a single internal output slab buffer of size
176
`chunkSize`, which defaults to 16K.
Oct 14, 2011
Oct 14, 2011
177
May 16, 2016
May 16, 2016
178
The speed of `zlib` compression is affected most dramatically by the
Oct 14, 2011
Oct 14, 2011
179
`level` setting. A higher level will result in better compression, but
180
will take longer to complete. A lower level will result in less
181
compression, but will be much faster.
182
May 16, 2016
May 16, 2016
183
In general, greater memory usage options will mean that Node.js has to make
184
fewer calls to `zlib` because it will be able to process more data on
185
each `write` operation. So, this is another factor that affects the
Oct 14, 2011
Oct 14, 2011
186
speed, at the cost of memory usage.
Jun 15, 2012
Jun 15, 2012
187
Apr 20, 2016
Apr 20, 2016
188
## Flushing
189
May 16, 2016
May 16, 2016
190
Calling [`.flush()`][] on a compression stream will make `zlib` return as much
Apr 20, 2016
Apr 20, 2016
191
output as currently possible. This may come at the cost of degraded compression
192
quality, but can be useful when data needs to be available as soon as possible.
193
194
In the following example, `flush()` is used to write a compressed partial
195
HTTP response to the client:
196
```js
197
const zlib = require('zlib');
198
const http = require('http');
199
200
http.createServer((request, response) => {
201
// For the sake of simplicity, the Accept-Encoding checks are omitted.
202
response.writeHead(200, { 'content-encoding': 'gzip' });
203
const output = zlib.createGzip();
204
output.pipe(response);
205
206
setInterval(() => {
207
output.write(`The current time is ${Date()}\n`, () => {
208
// The data has been passed to zlib, but the compression algorithm may
209
// have decided to buffer the data for more efficient compression.
210
// Calling .flush() will make the data available as soon as the client
211
// is ready to receive it.
212
output.flush();
213
});
214
}, 1000);
215
}).listen(1337);
216
```
217
Jun 15, 2012
Jun 15, 2012
218
## Constants
May 23, 2016
May 23, 2016
219
<!-- YAML
220
added: v0.5.8
221
-->
Jun 15, 2012
Jun 15, 2012
222
223
<!--type=misc-->
224
Jun 12, 2016
Jun 12, 2016
225
All of the constants defined in `zlib.h` are also defined on
226
`require('zlib').constants`. In the normal course of operations, it will not be
227
necessary to use these constants. They are documented so that their presence is
228
not surprising. This section is taken almost directly from the
229
[zlib documentation][]. See <http://zlib.net/manual.html#Constants> for more
230
details.
231
Nov 18, 2016
Nov 18, 2016
232
*Note*: Previously, the constants were available directly from
Jun 12, 2016
Jun 12, 2016
233
`require('zlib')`, for instance `zlib.Z_NO_FLUSH`. Accessing the constants
234
directly from the module is currently still possible but should be considered
235
deprecated.
Jun 15, 2012
Jun 15, 2012
236
237
Allowed flush values.
238
Jun 12, 2016
Jun 12, 2016
239
* `zlib.constants.Z_NO_FLUSH`
240
* `zlib.constants.Z_PARTIAL_FLUSH`
241
* `zlib.constants.Z_SYNC_FLUSH`
242
* `zlib.constants.Z_FULL_FLUSH`
243
* `zlib.constants.Z_FINISH`
244
* `zlib.constants.Z_BLOCK`
245
* `zlib.constants.Z_TREES`
Jun 15, 2012
Jun 15, 2012
246
247
Return codes for the compression/decompression functions. Negative
248
values are errors, positive values are used for special but normal
249
events.
250
Jun 12, 2016
Jun 12, 2016
251
* `zlib.constants.Z_OK`
252
* `zlib.constants.Z_STREAM_END`
253
* `zlib.constants.Z_NEED_DICT`
254
* `zlib.constants.Z_ERRNO`
255
* `zlib.constants.Z_STREAM_ERROR`
256
* `zlib.constants.Z_DATA_ERROR`
257
* `zlib.constants.Z_MEM_ERROR`
258
* `zlib.constants.Z_BUF_ERROR`
259
* `zlib.constants.Z_VERSION_ERROR`
Jun 15, 2012
Jun 15, 2012
260
261
Compression levels.
262
Jun 12, 2016
Jun 12, 2016
263
* `zlib.constants.Z_NO_COMPRESSION`
264
* `zlib.constants.Z_BEST_SPEED`
265
* `zlib.constants.Z_BEST_COMPRESSION`
266
* `zlib.constants.Z_DEFAULT_COMPRESSION`
Jun 15, 2012
Jun 15, 2012
267
268
Compression strategy.
269
Jun 12, 2016
Jun 12, 2016
270
* `zlib.constants.Z_FILTERED`
271
* `zlib.constants.Z_HUFFMAN_ONLY`
272
* `zlib.constants.Z_RLE`
273
* `zlib.constants.Z_FIXED`
274
* `zlib.constants.Z_DEFAULT_STRATEGY`
Nov 13, 2015
Nov 13, 2015
275
276
## Class Options
May 23, 2016
May 23, 2016
277
<!-- YAML
278
added: v0.11.1
Feb 24, 2017
Feb 24, 2017
279
changes:
May 30, 2017
May 30, 2017
280
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
281
pr-url: https://github.com/nodejs/node/pull/12001
282
description: The `dictionary` option can be an Uint8Array now.
Feb 24, 2017
Feb 24, 2017
283
- version: v5.11.0
284
pr-url: https://github.com/nodejs/node/pull/6069
285
description: The `finishFlush` option is supported now.
May 23, 2016
May 23, 2016
286
-->
Nov 13, 2015
Nov 13, 2015
287
288
<!--type=misc-->
289
May 16, 2016
May 16, 2016
290
Each class takes an `options` object. All options are optional.
Nov 13, 2015
Nov 13, 2015
291
292
Note that some options are only relevant when compressing, and are
293
ignored by the decompression classes.
294
Apr 3, 2017
Apr 3, 2017
295
* `flush` {integer} (default: `zlib.constants.Z_NO_FLUSH`)
296
* `finishFlush` {integer} (default: `zlib.constants.Z_FINISH`)
297
* `chunkSize` {integer} (default: 16\*1024)
298
* `windowBits` {integer}
299
* `level` {integer} (compression only)
300
* `memLevel` {integer} (compression only)
301
* `strategy` {integer} (compression only)
Apr 12, 2017
Apr 12, 2017
302
* `dictionary` {Buffer|TypedArray|DataView} (deflate/inflate only, empty dictionary by
Apr 3, 2017
Apr 3, 2017
303
default)
Nov 13, 2015
Nov 13, 2015
304
305
See the description of `deflateInit2` and `inflateInit2` at
306
<http://zlib.net/manual.html#Advanced> for more information on these.
307
308
## Class: zlib.Deflate
May 23, 2016
May 23, 2016
309
<!-- YAML
310
added: v0.5.8
311
-->
Nov 13, 2015
Nov 13, 2015
312
313
Compress data using deflate.
314
315
## Class: zlib.DeflateRaw
May 23, 2016
May 23, 2016
316
<!-- YAML
317
added: v0.5.8
318
-->
Nov 13, 2015
Nov 13, 2015
319
May 16, 2016
May 16, 2016
320
Compress data using deflate, and do not append a `zlib` header.
Nov 13, 2015
Nov 13, 2015
321
322
## Class: zlib.Gunzip
May 23, 2016
May 23, 2016
323
<!-- YAML
324
added: v0.5.8
Feb 24, 2017
Feb 24, 2017
325
changes:
326
- version: v6.0.0
327
pr-url: https://github.com/nodejs/node/pull/5883
328
description: Trailing garbage at the end of the input stream will now
329
result in an `error` event.
330
- version: v5.9.0
331
pr-url: https://github.com/nodejs/node/pull/5120
332
description: Multiple concatenated gzip file members are supported now.
333
- version: v5.0.0
334
pr-url: https://github.com/nodejs/node/pull/2595
335
description: A truncated input stream will now result in an `error` event.
May 23, 2016
May 23, 2016
336
-->
Nov 13, 2015
Nov 13, 2015
337
338
Decompress a gzip stream.
339
340
## Class: zlib.Gzip
May 23, 2016
May 23, 2016
341
<!-- YAML
342
added: v0.5.8
343
-->
Nov 13, 2015
Nov 13, 2015
344
345
Compress data using gzip.
346
347
## Class: zlib.Inflate
May 23, 2016
May 23, 2016
348
<!-- YAML
349
added: v0.5.8
Feb 24, 2017
Feb 24, 2017
350
changes:
351
- version: v5.0.0
352
pr-url: https://github.com/nodejs/node/pull/2595
353
description: A truncated input stream will now result in an `error` event.
May 23, 2016
May 23, 2016
354
-->
Nov 13, 2015
Nov 13, 2015
355
356
Decompress a deflate stream.
357
358
## Class: zlib.InflateRaw
May 23, 2016
May 23, 2016
359
<!-- YAML
360
added: v0.5.8
Feb 24, 2017
Feb 24, 2017
361
changes:
362
- version: v6.8.0
363
pr-url: https://github.com/nodejs/node/pull/8512
364
description: Custom dictionaries are now supported by `InflateRaw`.
365
- version: v5.0.0
366
pr-url: https://github.com/nodejs/node/pull/2595
367
description: A truncated input stream will now result in an `error` event.
May 23, 2016
May 23, 2016
368
-->
Nov 13, 2015
Nov 13, 2015
369
370
Decompress a raw deflate stream.
371
372
## Class: zlib.Unzip
May 23, 2016
May 23, 2016
373
<!-- YAML
374
added: v0.5.8
375
-->
Nov 13, 2015
Nov 13, 2015
376
377
Decompress either a Gzip- or Deflate-compressed stream by auto-detecting
378
the header.
379
380
## Class: zlib.Zlib
May 23, 2016
May 23, 2016
381
<!-- YAML
382
added: v0.5.8
383
-->
Nov 13, 2015
Nov 13, 2015
384
385
Not exported by the `zlib` module. It is documented here because it is the base
386
class of the compressor/decompressor classes.
387
388
### zlib.flush([kind], callback)
May 23, 2016
May 23, 2016
389
<!-- YAML
390
added: v0.5.8
391
-->
Nov 13, 2015
Nov 13, 2015
392
Jun 12, 2016
Jun 12, 2016
393
`kind` defaults to `zlib.constants.Z_FULL_FLUSH`.
Nov 13, 2015
Nov 13, 2015
394
395
Flush pending data. Don't call this frivolously, premature flushes negatively
396
impact the effectiveness of the compression algorithm.
397
May 16, 2016
May 16, 2016
398
Calling this only flushes data from the internal `zlib` state, and does not
Apr 20, 2016
Apr 20, 2016
399
perform flushing of any kind on the streams level. Rather, it behaves like a
400
normal call to `.write()`, i.e. it will be queued up behind other pending
401
writes and will only produce output when data is being read from the stream.
402
Nov 13, 2015
Nov 13, 2015
403
### zlib.params(level, strategy, callback)
May 23, 2016
May 23, 2016
404
<!-- YAML
405
added: v0.11.4
406
-->
Nov 13, 2015
Nov 13, 2015
407
408
Dynamically update the compression level and compression strategy.
409
Only applicable to deflate algorithm.
410
411
### zlib.reset()
May 23, 2016
May 23, 2016
412
<!-- YAML
413
added: v0.7.0
414
-->
Nov 13, 2015
Nov 13, 2015
415
Nov 2, 2016
Nov 2, 2016
416
Reset the compressor/decompressor to factory defaults. Only applicable to
417
the inflate and deflate algorithms.
418
Jun 12, 2016
Jun 12, 2016
419
## zlib.constants
Jan 18, 2017
Jan 18, 2017
420
<!-- YAML
421
added: v7.0.0
422
-->
Jun 12, 2016
Jun 12, 2016
423
Jul 14, 2016
Jul 14, 2016
424
Provides an object enumerating Zlib-related constants.
Jun 12, 2016
Jun 12, 2016
425
Nov 13, 2015
Nov 13, 2015
426
## zlib.createDeflate([options])
May 23, 2016
May 23, 2016
427
<!-- YAML
428
added: v0.5.8
429
-->
Nov 13, 2015
Nov 13, 2015
430
Nov 16, 2015
Nov 16, 2015
431
Returns a new [Deflate][] object with an [options][].
Nov 13, 2015
Nov 13, 2015
432
433
## zlib.createDeflateRaw([options])
May 23, 2016
May 23, 2016
434
<!-- YAML
435
added: v0.5.8
436
-->
Nov 13, 2015
Nov 13, 2015
437
Nov 16, 2015
Nov 16, 2015
438
Returns a new [DeflateRaw][] object with an [options][].
Nov 13, 2015
Nov 13, 2015
439
May 25, 2017
May 25, 2017
440
*Note*: The zlib library rejects requests for 256-byte windows (i.e.,
May 21, 2017
May 21, 2017
441
`{ windowBits: 8 }` in `options`). An `Error` will be thrown when creating
442
a [DeflateRaw][] object with this specific value of the `windowBits` option.
443
Nov 13, 2015
Nov 13, 2015
444
## zlib.createGunzip([options])
May 23, 2016
May 23, 2016
445
<!-- YAML
446
added: v0.5.8
447
-->
Nov 13, 2015
Nov 13, 2015
448
Nov 16, 2015
Nov 16, 2015
449
Returns a new [Gunzip][] object with an [options][].
Nov 13, 2015
Nov 13, 2015
450
451
## zlib.createGzip([options])
May 23, 2016
May 23, 2016
452
<!-- YAML
453
added: v0.5.8
454
-->
Nov 13, 2015
Nov 13, 2015
455
Nov 16, 2015
Nov 16, 2015
456
Returns a new [Gzip][] object with an [options][].
Nov 13, 2015
Nov 13, 2015
457
458
## zlib.createInflate([options])
May 23, 2016
May 23, 2016
459
<!-- YAML
460
added: v0.5.8
461
-->
Nov 13, 2015
Nov 13, 2015
462
Nov 16, 2015
Nov 16, 2015
463
Returns a new [Inflate][] object with an [options][].
Nov 13, 2015
Nov 13, 2015
464
465
## zlib.createInflateRaw([options])
May 23, 2016
May 23, 2016
466
<!-- YAML
467
added: v0.5.8
468
-->
Nov 13, 2015
Nov 13, 2015
469
Nov 16, 2015
Nov 16, 2015
470
Returns a new [InflateRaw][] object with an [options][].
Nov 13, 2015
Nov 13, 2015
471
472
## zlib.createUnzip([options])
May 23, 2016
May 23, 2016
473
<!-- YAML
474
added: v0.5.8
475
-->
Nov 13, 2015
Nov 13, 2015
476
Nov 16, 2015
Nov 16, 2015
477
Returns a new [Unzip][] object with an [options][].
Nov 13, 2015
Nov 13, 2015
478
479
## Convenience Methods
480
481
<!--type=misc-->
482
Apr 12, 2017
Apr 12, 2017
483
All of these take a [`Buffer`][], [`TypedArray`][], [`DataView`][], or string as
484
the first argument, an optional second argument to supply options to the `zlib`
485
classes and will call the supplied callback with `callback(error, result)`.
Nov 13, 2015
Nov 13, 2015
486
487
Every method has a `*Sync` counterpart, which accept the same arguments, but
488
without a callback.
489
Apr 3, 2017
Apr 3, 2017
490
### zlib.deflate(buffer[, options], callback)
May 23, 2016
May 23, 2016
491
<!-- YAML
492
added: v0.6.0
Apr 3, 2017
Apr 3, 2017
493
changes:
May 30, 2017
May 30, 2017
494
- version: v8.0.0
495
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
496
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
497
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
498
pr-url: https://github.com/nodejs/node/pull/12001
499
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
500
-->
Apr 3, 2017
Apr 3, 2017
501
### zlib.deflateSync(buffer[, options])
May 23, 2016
May 23, 2016
502
<!-- YAML
503
added: v0.11.12
Apr 3, 2017
Apr 3, 2017
504
changes:
May 30, 2017
May 30, 2017
505
- version: v8.0.0
506
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
507
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
508
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
509
pr-url: https://github.com/nodejs/node/pull/12001
510
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
511
-->
Nov 13, 2015
Nov 13, 2015
512
Apr 12, 2017
Apr 12, 2017
513
- `buffer` {Buffer|TypedArray|DataView|string}
Apr 3, 2017
Apr 3, 2017
514
515
Compress a chunk of data with [Deflate][].
Nov 13, 2015
Nov 13, 2015
516
Apr 3, 2017
Apr 3, 2017
517
### zlib.deflateRaw(buffer[, options], callback)
May 23, 2016
May 23, 2016
518
<!-- YAML
519
added: v0.6.0
Apr 3, 2017
Apr 3, 2017
520
changes:
May 30, 2017
May 30, 2017
521
- version: v8.0.0
522
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
523
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
524
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
525
pr-url: https://github.com/nodejs/node/pull/12001
526
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
527
-->
Apr 3, 2017
Apr 3, 2017
528
### zlib.deflateRawSync(buffer[, options])
May 23, 2016
May 23, 2016
529
<!-- YAML
530
added: v0.11.12
Apr 3, 2017
Apr 3, 2017
531
changes:
May 30, 2017
May 30, 2017
532
- version: v8.0.0
533
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
534
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
535
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
536
pr-url: https://github.com/nodejs/node/pull/12001
537
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
538
-->
Nov 13, 2015
Nov 13, 2015
539
Apr 12, 2017
Apr 12, 2017
540
- `buffer` {Buffer|TypedArray|DataView|string}
Apr 3, 2017
Apr 3, 2017
541
542
Compress a chunk of data with [DeflateRaw][].
Nov 13, 2015
Nov 13, 2015
543
Apr 3, 2017
Apr 3, 2017
544
### zlib.gunzip(buffer[, options], callback)
May 23, 2016
May 23, 2016
545
<!-- YAML
546
added: v0.6.0
Apr 3, 2017
Apr 3, 2017
547
changes:
May 30, 2017
May 30, 2017
548
- version: v8.0.0
549
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
550
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
551
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
552
pr-url: https://github.com/nodejs/node/pull/12001
553
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
554
-->
Apr 3, 2017
Apr 3, 2017
555
### zlib.gunzipSync(buffer[, options])
May 23, 2016
May 23, 2016
556
<!-- YAML
557
added: v0.11.12
Apr 3, 2017
Apr 3, 2017
558
changes:
May 30, 2017
May 30, 2017
559
- version: v8.0.0
560
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
561
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
562
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
563
pr-url: https://github.com/nodejs/node/pull/12001
564
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
565
-->
Nov 13, 2015
Nov 13, 2015
566
Apr 12, 2017
Apr 12, 2017
567
- `buffer` {Buffer|TypedArray|DataView|string}
Apr 3, 2017
Apr 3, 2017
568
569
Decompress a chunk of data with [Gunzip][].
Nov 13, 2015
Nov 13, 2015
570
Apr 3, 2017
Apr 3, 2017
571
### zlib.gzip(buffer[, options], callback)
May 23, 2016
May 23, 2016
572
<!-- YAML
573
added: v0.6.0
Apr 3, 2017
Apr 3, 2017
574
changes:
May 30, 2017
May 30, 2017
575
- version: v8.0.0
576
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
577
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
578
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
579
pr-url: https://github.com/nodejs/node/pull/12001
580
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
581
-->
Apr 3, 2017
Apr 3, 2017
582
### zlib.gzipSync(buffer[, options])
May 23, 2016
May 23, 2016
583
<!-- YAML
584
added: v0.11.12
Apr 3, 2017
Apr 3, 2017
585
changes:
May 30, 2017
May 30, 2017
586
- version: v8.0.0
587
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
588
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
589
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
590
pr-url: https://github.com/nodejs/node/pull/12001
591
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
592
-->
Nov 13, 2015
Nov 13, 2015
593
Apr 12, 2017
Apr 12, 2017
594
- `buffer` {Buffer|TypedArray|DataView|string}
Nov 13, 2015
Nov 13, 2015
595
Apr 3, 2017
Apr 3, 2017
596
Compress a chunk of data with [Gzip][].
597
598
### zlib.inflate(buffer[, options], callback)
May 23, 2016
May 23, 2016
599
<!-- YAML
600
added: v0.6.0
Apr 3, 2017
Apr 3, 2017
601
changes:
May 30, 2017
May 30, 2017
602
- version: v8.0.0
603
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
604
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
605
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
606
pr-url: https://github.com/nodejs/node/pull/12001
607
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
608
-->
Apr 3, 2017
Apr 3, 2017
609
### zlib.inflateSync(buffer[, options])
May 23, 2016
May 23, 2016
610
<!-- YAML
611
added: v0.11.12
Apr 3, 2017
Apr 3, 2017
612
changes:
May 30, 2017
May 30, 2017
613
- version: v8.0.0
614
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
615
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
616
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
617
pr-url: https://github.com/nodejs/node/pull/12001
618
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
619
-->
Nov 13, 2015
Nov 13, 2015
620
Apr 12, 2017
Apr 12, 2017
621
- `buffer` {Buffer|TypedArray|DataView|string}
Nov 13, 2015
Nov 13, 2015
622
Apr 3, 2017
Apr 3, 2017
623
Decompress a chunk of data with [Inflate][].
624
625
### zlib.inflateRaw(buffer[, options], callback)
May 23, 2016
May 23, 2016
626
<!-- YAML
627
added: v0.6.0
Apr 3, 2017
Apr 3, 2017
628
changes:
May 30, 2017
May 30, 2017
629
- version: v8.0.0
630
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
631
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
632
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
633
pr-url: https://github.com/nodejs/node/pull/12001
634
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
635
-->
Apr 3, 2017
Apr 3, 2017
636
### zlib.inflateRawSync(buffer[, options])
May 23, 2016
May 23, 2016
637
<!-- YAML
638
added: v0.11.12
Apr 3, 2017
Apr 3, 2017
639
changes:
May 30, 2017
May 30, 2017
640
- version: v8.0.0
641
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
642
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
643
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
644
pr-url: https://github.com/nodejs/node/pull/12001
645
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
646
-->
Nov 13, 2015
Nov 13, 2015
647
Apr 12, 2017
Apr 12, 2017
648
- `buffer` {Buffer|TypedArray|DataView|string}
Apr 3, 2017
Apr 3, 2017
649
650
Decompress a chunk of data with [InflateRaw][].
Nov 13, 2015
Nov 13, 2015
651
Apr 3, 2017
Apr 3, 2017
652
### zlib.unzip(buffer[, options], callback)
May 23, 2016
May 23, 2016
653
<!-- YAML
654
added: v0.6.0
Apr 3, 2017
Apr 3, 2017
655
changes:
May 30, 2017
May 30, 2017
656
- version: v8.0.0
657
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
658
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
659
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
660
pr-url: https://github.com/nodejs/node/pull/12001
661
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
662
-->
Apr 3, 2017
Apr 3, 2017
663
### zlib.unzipSync(buffer[, options])
May 23, 2016
May 23, 2016
664
<!-- YAML
665
added: v0.11.12
Apr 3, 2017
Apr 3, 2017
666
changes:
May 30, 2017
May 30, 2017
667
- version: v8.0.0
668
pr-url: https://github.com/nodejs/node/pull/12223
Apr 12, 2017
Apr 12, 2017
669
description: The `buffer` parameter can be any TypedArray or DataView now.
May 30, 2017
May 30, 2017
670
- version: v8.0.0
Apr 3, 2017
Apr 3, 2017
671
pr-url: https://github.com/nodejs/node/pull/12001
672
description: The `buffer` parameter can be an Uint8Array now.
May 23, 2016
May 23, 2016
673
-->
Nov 13, 2015
Nov 13, 2015
674
Apr 12, 2017
Apr 12, 2017
675
- `buffer` {Buffer|TypedArray|DataView|string}
Apr 3, 2017
Apr 3, 2017
676
677
Decompress a chunk of data with [Unzip][].
Nov 16, 2015
Nov 16, 2015
678
May 8, 2017
May 8, 2017
679
[`.flush()`]: #zlib_zlib_flush_kind_callback
May 16, 2016
May 16, 2016
680
[`Accept-Encoding`]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
May 8, 2017
May 8, 2017
681
[`Buffer`]: buffer.html#buffer_class_buffer
May 16, 2016
May 16, 2016
682
[`Content-Encoding`]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
May 8, 2017
May 8, 2017
683
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
684
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
Nov 16, 2015
Nov 16, 2015
685
[DeflateRaw]: #zlib_class_zlib_deflateraw
May 8, 2017
May 8, 2017
686
[Deflate]: #zlib_class_zlib_deflate
Nov 16, 2015
Nov 16, 2015
687
[Gunzip]: #zlib_class_zlib_gunzip
688
[Gzip]: #zlib_class_zlib_gzip
689
[InflateRaw]: #zlib_class_zlib_inflateraw
May 8, 2017
May 8, 2017
690
[Inflate]: #zlib_class_zlib_inflate
691
[Memory Usage Tuning]: #zlib_memory_usage_tuning
Nov 16, 2015
Nov 16, 2015
692
[Unzip]: #zlib_class_zlib_unzip
May 8, 2017
May 8, 2017
693
[options]: #zlib_class_options
694
[zlib documentation]: http://zlib.net/manual.html#Constants