Module basic module

Module zlib

zlib is a built-in compression module that supports multiple compression formats and modes such as gzip, deflate, and zlib.

zlib mainly consists of the following three functions:

  • deflate: compressed data;
  • inflate: decompress data;
  • gzip: gzip compression format.

Before using zlib, you need to choose one of them according to the compression algorithm you need to use. You can refer to the constants of zlib to select the corresponding compression algorithm. For example, we use the deflate compression algorithm for module description:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const zlib = require('zlib'); const { NO_COMPRESSION, BEST_SPEED, BEST_COMPRESSION, DEFAULT_COMPRESSION } = require('zlib'); // compress data const deflated = zlib.deflate('hello, world', BEST_SPEED); console.log(deflated.toString()); // decompress data const inflated = zlib.inflate(deflated); console.log(inflated.toString());

The above code shows how to compress and decompress data. First use zlib.deflatethe method to compress hello, worldthe string, and pass in BEST_SPEEDas the compression level option, and then use zlib.inflatethe method to decompress the data. The output result should be the same as the original string.

zlib.deflateand zlib.inflateboth support defining the compression level. The compression level is a number with a value range of [NO_COMPRESSION, BEST_SPEED, DEFAULT_COMPRESSION, BEST_COMPRESSION]. The default value is DEFAULT_COMPRESSION. For the meaning of these four compression levels, you can refer to the following table:

Compression Level Meaning
zlib.NO_COMPRESSION Uncompressed data (with support for compression header completion)
zlib.BEST_SPEED The fastest compression speed; but the compression ratio is correspondingly worse.
zlib.DEFAULT_COMPRESSION According to the default value of the compression algorithm, it is usually slower than BEST_SPEED but has a higher compression rate.
zlib.BEST_COMPRESSION Highest compression ratio, but correspondingly slower compression speed.

zlibWhat needs to be noted when using the module is that if you want to compress and decompress data at the same time, it is recommended to use it deflateto compress the data first and then use it inflateto decompress the data to avoid errors. For different compression formats and algorithms, there are other classes and methods for compression and decompression. You can refer to the following documents for use.

static function

createDeflate

Create a deflate stream object

1
static Stream zlib.createDeflate(Stream to);

Call parameters:

  • to:Stream, a stream used to store processing results

Return results:

  • Stream, returns the encapsulated stream object

createDeflateRaw

Create a deflateRaw stream object

1
static Stream zlib.createDeflateRaw(Stream to);

Call parameters:

  • to:Stream, a stream used to store processing results

Return results:

  • Stream, returns the encapsulated stream object

createGunzip

Create a gunzip stream object

1 2
static Stream zlib.createGunzip(Stream to, Integer maxSize = -1);

Call parameters:

  • to:Stream, a stream used to store processing results
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Return results:

  • Stream, returns the encapsulated stream object

createGzip

Create a gzip stream object

1
static Stream zlib.createGzip(Stream to);

Call parameters:

  • to:Stream, a stream used to store processing results

Return results:

  • Stream, returns the encapsulated stream object

createInflate

Create an inflate stream object

1 2
static Stream zlib.createInflate(Stream to, Integer maxSize = -1);

Call parameters:

  • to:Stream, a stream used to store processing results
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Return results:

  • Stream, returns the encapsulated stream object

createInflateRaw

Create an inflateRaw stream object

1 2
static Stream zlib.createInflateRaw(Stream to, Integer maxSize = -1);

Call parameters:

  • to:Stream, a stream used to store processing results
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Return results:

  • Stream, returns the encapsulated stream object

deflate

Use deflate algorithm to compress data (zlib format)

1 2
static Buffer zlib.deflate(Buffer data, Integer level = DEFAULT_COMPRESSION) async;

Call parameters:

  • data:Buffer, given the data to be compressed
  • level: Integer, specifies the compression level, the default is DEFAULT_COMPRESSION

Return results:

  • Buffer, returns compressed binary data

deflateTo

Use deflate algorithm to compress data into stream objects (zlib format)

1 2 3
static zlib.deflateTo(Buffer data, Stream stm, Integer level = DEFAULT_COMPRESSION) async;

Call parameters:

  • data:Buffer, given the data to be compressed
  • stm:Stream, specifies the stream to store compressed data
  • level: Integer, specifies the compression level, the default is DEFAULT_COMPRESSION

Use the deflate algorithm to compress data in the source stream into a stream object (zlib format)

1 2 3
static zlib.deflateTo(Stream src, Stream stm, Integer level = DEFAULT_COMPRESSION) async;

Call parameters:

  • src:Stream, given the stream in which the data to be compressed is located
  • stm:Stream, specifies the stream to store compressed data
  • level: Integer, specifies the compression level, the default is DEFAULT_COMPRESSION

inflate

Decompress data compressed by deflate algorithm (zlib format)

1 2
static Buffer zlib.inflate(Buffer data, Integer maxSize = -1) async;

Call parameters:

  • data:Buffer, given the compressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Return results:

  • Buffer, returns the decompressed binary data

inflateTo

Decompress data compressed by the deflate algorithm into a stream object (zlib format)

1 2 3
static zlib.inflateTo(Buffer data, Stream stm, Integer maxSize = -1) async;

Call parameters:

  • data:Buffer, given the data to be decompressed
  • stm:Stream, specify the stream to store decompressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Decompress the data compressed by the deflate algorithm in the source stream into a stream object (zlib format)

1 2 3
static zlib.inflateTo(Stream src, Stream stm, Integer maxSize = -1) async;

Call parameters:

  • src:Stream, given the stream in which the data to be decompressed is located
  • stm:Stream, specify the stream to store decompressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

gzip

Compress data using gzip algorithm

1
static Buffer zlib.gzip(Buffer data) async;

Call parameters:

  • data:Buffer, given the data to be compressed

Return results:

  • Buffer, returns compressed binary data

gzipTo

Use the gzip algorithm to compress data into a stream object

1 2
static zlib.gzipTo(Buffer data, Stream stm) async;

Call parameters:

  • data:Buffer, given the data to be compressed
  • stm:Stream, specifies the stream to store compressed data

Use the gzip algorithm to compress the data in the source stream into a stream object.

1 2
static zlib.gzipTo(Stream src, Stream stm) async;

Call parameters:

  • src:Stream, given the stream in which the data to be compressed is located
  • stm:Stream, specifies the stream to store compressed data

gunzip

Decompress data compressed by the gzip algorithm

1 2
static Buffer zlib.gunzip(Buffer data, Integer maxSize = -1) async;

Call parameters:

  • data:Buffer, given the compressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Return results:

  • Buffer, returns the decompressed binary data

gunzipTo

Decompress gzip algorithm-compressed data into a stream object

1 2 3
static zlib.gunzipTo(Buffer data, Stream stm, Integer maxSize = -1) async;

Call parameters:

  • data:Buffer, given the data to be decompressed
  • stm:Stream, specify the stream to store decompressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Decompress the gzip algorithm-compressed data in the source stream into a stream object

1 2 3
static zlib.gunzipTo(Stream src, Stream stm, Integer maxSize = -1) async;

Call parameters:

  • src:Stream, given the stream in which the data to be decompressed is located
  • stm:Stream, specify the stream to store decompressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

deflateRaw

Compress data using deflate algorithm (deflateRaw)

1 2
static Buffer zlib.deflateRaw(Buffer data, Integer level = DEFAULT_COMPRESSION) async;

Call parameters:

  • data:Buffer, given the data to be compressed
  • level: Integer, specifies the compression level, the default is DEFAULT_COMPRESSION

Return results:

  • Buffer, returns compressed binary data

deflateRawTo

Use deflate algorithm to compress data into stream objects (deflateRaw)

1 2 3
static zlib.deflateRawTo(Buffer data, Stream stm, Integer level = DEFAULT_COMPRESSION) async;

Call parameters:

  • data:Buffer, given the data to be compressed
  • stm:Stream, specifies the stream to store compressed data
  • level: Integer, specifies the compression level, the default is DEFAULT_COMPRESSION

Use the deflate algorithm to compress data in the source stream into a stream object (deflateRaw)

1 2 3
static zlib.deflateRawTo(Stream src, Stream stm, Integer level = DEFAULT_COMPRESSION) async;

Call parameters:

  • src:Stream, given the stream in which the data to be compressed is located
  • stm:Stream, specifies the stream to store compressed data
  • level: Integer, specifies the compression level, the default is DEFAULT_COMPRESSION

inflateRaw

Decompress data compressed by deflate algorithm (inflateRaw)

1 2
static Buffer zlib.inflateRaw(Buffer data, Integer maxSize = -1) async;

Call parameters:

  • data:Buffer, given the compressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Return results:

  • Buffer, returns the decompressed binary data

inflateRawTo

Decompress data compressed by the deflate algorithm into a stream object (inflateRaw)

1 2 3
static zlib.inflateRawTo(Buffer data, Stream stm, Integer maxSize = -1) async;

Call parameters:

  • data:Buffer, given the data to be decompressed
  • stm:Stream, specify the stream to store decompressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

Decompress data compressed by the deflate algorithm in the source stream into a stream object (inflateRaw)

1 2 3
static zlib.inflateRawTo(Stream src, Stream stm, Integer maxSize = -1) async;

Call parameters:

  • src:Stream, given the stream in which the data to be decompressed is located
  • stm:Stream, specify the stream to store decompressed data
  • maxSize: Integer, specifies the decompression size limit, the default is -1, no limit

constant

NO_COMPRESSION

deflate compression level, set no compression

1
const zlib.NO_COMPRESSION = 0;

BEST_SPEED

deflate compression level, set the fastest compression

1
const zlib.BEST_SPEED = 1;

BEST_COMPRESSION

deflate compression level, set the highest compression

1
const zlib.BEST_COMPRESSION = 9;

DEFAULT_COMPRESSION

deflate compression level, set default settings

1
const zlib.DEFAULT_COMPRESSION = -1;