zlib

index

Overview

Zlib compression and decompression built on miniz.

Provides raw deflate/inflate, zlib-wrapped compress/decompress, gzip/gunzip, and CRC-32/Adler-32 checksum functions.

Functions

NameSignature
deflatedeflate(data, level) -> compressed, err
compresscompress(data, level) -> compressed, err
gzipgzip(data, level) -> compressed, err
inflateinflate(data, max_size) -> decompressed, bytes_consumed, err
decompressdecompress(data, max_size) -> decompressed, bytes_consumed, err
decompress_headdecompress_head(data, n) -> head, err
gunzipgunzip(data, max_size) -> decompressed, bytes_consumed, err
crc32crc32(data, initial) -> checksum
adler32adler32(data, initial) -> checksum

deflate(data, level) -> compressed, err

Compress data using raw deflate (no header/trailer)

Level ranges from 0 (no compression) to 10 (maximum), default 6.

compress(data, level) -> compressed, err

Compress data with zlib wrapping (header + adler32 footer)

Level ranges from 0 (no compression) to 10 (maximum), default 6.

gzip(data, level) -> compressed, err

Compress data in gzip format (RFC 1952)

Level ranges from 0 (no compression) to 10 (maximum), default 6. Output is compatible with the gzip command-line tool.

inflate(data, max_size) -> decompressed, bytes_consumed, err

Decompress raw deflate data

Returns the decompressed data and the number of compressed bytes consumed from the input. max_size caps the output, 64 MB by default as a guard against decompression bombs. A caller that knows the size may pass a larger cap. An output past the cap returns nil. Input that ends before the stream does returns nil and "truncated stream".

decompress(data, max_size) -> decompressed, bytes_consumed, err

Decompress zlib-wrapped data

Input must include the zlib header and adler32 footer. Returns the decompressed data and the number of bytes consumed from the input. max_size caps the output, 64 MB by default; a larger cap is honoured, as in inflate. Input that ends before the stream does returns nil and "truncated stream".

decompress_head(data, n) -> head, err

Decompress the first bytes of zlib-wrapped data

Returns at most n bytes from the start of the output, for a header read without inflating the whole stream. A stream longer than n is no error, and input cut after n bytes of output is none either: the rest of the stream is not read, and its adler32 trailer is not checked. A stream shorter than n returns its whole output. Input that ends before both the stream and n bytes returns nil and "truncated stream". An n under 1 returns "".

gunzip(data, max_size) -> decompressed, bytes_consumed, err

Decompress gzip data (RFC 1952)

Parses the gzip header, inflates the payload, and validates the CRC32 trailer. Compatible with output from the gzip command. Returns the decompressed data and total bytes consumed. max_size caps the output, 64 MB by default; a larger cap is honoured, as in inflate. A payload that ends before its deflate stream does returns nil and "truncated stream".

crc32(data, initial) -> checksum

Compute CRC-32 checksum

Returns the CRC-32 value as a number. Pass a previous result as initial to compute a running checksum over multiple chunks.

adler32(data, initial) -> checksum

Compute Adler-32 checksum

Returns the Adler-32 value as a number. Pass a previous result as initial to compute a running checksum over multiple chunks.