Erlang Run-Time System Application (ERTS)

Reference Manual

Version 9.0

Table of Contents

zlib

Module

zlib

Module Summary

zlib compression interface.

Description

This module provides an API for the zlib library (www.zlib.net). It is used to compress and decompress data. The data format is described by RFC 1950, RFC 1951, and RFC 1952.

A typical (compress) usage is as follows:

Z = zlib:open(),
ok = zlib:deflateInit(Z,default),

Compress = fun(end_of_data, _Cont) -> [];
              (Data, Cont) ->
                 [zlib:deflate(Z, Data)|Cont(Read(),Cont)]
           end,
Compressed = Compress(Read(),Compress),
Last = zlib:deflate(Z, [], finish),
ok = zlib:deflateEnd(Z),
zlib:close(Z),
list_to_binary([Compressed|Last])

In all functions errors, {'EXIT',{Reason,Backtrace}}, can be thrown, where Reason describes the error.

Typical Reasonss:

badarg
Bad argument.
data_error
The data contains errors.
stream_error
Inconsistent stream state.
einval
Bad value or wrong function called.
{need_dictionary,Adler32}
See inflate/2.

Data Types

zstream() = port()

A zlib stream, see open/0.

zlevel() =
    none | default | best_compression | best_speed | 0..9

zmemlevel() = 1..9
zmethod() = deflated
zstrategy() = default | filtered | huffman_only | rle
zwindowbits() = -15..-8 | 8..47

Normally in the range -15..-8 | 8..15.

Exports

adler32(Z, Data) -> CheckSum

Types

Data = iodata()
CheckSum = integer()

Calculates the Adler-32 checksum for Data.

adler32(Z, PrevAdler, Data) -> CheckSum

Types

PrevAdler = integer()
Data = iodata()
CheckSum = integer()

Updates a running Adler-32 checksum for Data. If Data is the empty binary or the empty iolist, this function returns the required initial value for the checksum.

Example:

Crc = lists:foldl(fun(Data,Crc0) ->
                      zlib:adler32(Z, Crc0, Data),
                  end, zlib:adler32(Z,<< >>), Datas)

adler32_combine(Z, Adler1, Adler2, Size2) -> Adler

Types

Adler = Adler1 = Adler2 = Size2 = integer()

Combines two Adler-32 checksums into one. For two binaries or iolists, Data1 and Data2 with sizes of Size1 and Size2, with Adler-32 checksums Adler1 and Adler2.

This function returns the Adler checksum of [Data1,Data2], requiring only Adler1, Adler2, and Size2.

close(Z) -> ok

Types

Closes the stream referenced by Z.

compress(Data) -> Compressed

Types

Data = iodata()
Compressed = binary()

Compresses data with zlib headers and checksum.

crc32(Z) -> CRC

Types

CRC = integer()

Gets the current calculated CRC checksum.

crc32(Z, Data) -> CRC

Types

Data = iodata()
CRC = integer()

Calculates the CRC checksum for Data.

crc32(Z, PrevCRC, Data) -> CRC

Types

PrevCRC = integer()
Data = iodata()
CRC = integer()

Updates a running CRC checksum for Data. If Data is the empty binary or the empty iolist, this function returns the required initial value for the CRC.

Example:

Crc = lists:foldl(fun(Data,Crc0) ->
                      zlib:crc32(Z, Crc0, Data),
                  end, zlib:crc32(Z,<< >>), Datas)

crc32_combine(Z, CRC1, CRC2, Size2) -> CRC

Types

CRC = CRC1 = CRC2 = Size2 = integer()

Combines two CRC checksums into one. For two binaries or iolists, Data1 and Data2 with sizes of Size1 and Size2, with CRC checksums CRC1 and CRC2.

This function returns the CRC checksum of [Data1,Data2], requiring only CRC1, CRC2, and Size2.

deflate(Z, Data) -> Compressed

Types

Data = iodata()
Compressed = iolist()

Same as deflate(Z, Data, none).

deflate(Z, Data, Flush) -> Compressed

Types

Data = iodata()
Flush = none | sync | full | finish
Compressed = iolist()

Compresses as much data as possible, and stops when the input buffer becomes empty. It can introduce some output latency (reading input without producing any output) except when forced to flush.

If Flush is set to sync, all pending output is flushed to the output buffer and the output is aligned on a byte boundary, so that the decompressor can get all input data available so far. Flushing can degrade compression for some compression algorithms; thus, use it only when necessary.

If Flush is set to full, all output is flushed as with sync, and the compression state is reset so that decompression can restart from this point if previous compressed data has been damaged or if random access is desired. Using full too often can seriously degrade the compression.

If Flush is set to finish, pending input is processed, pending output is flushed, and deflate/3 returns. Afterwards the only possible operations on the stream are deflateReset/1 or deflateEnd/1.

Flush can be set to finish immediately after deflateInit if all compression is to be done in one step.

Example:

zlib:deflateInit(Z),
B1 = zlib:deflate(Z,Data),
B2 = zlib:deflate(Z,<< >>,finish),
zlib:deflateEnd(Z),
list_to_binary([B1,B2])

deflateEnd(Z) -> ok

Types

Ends the deflate session and cleans all data used. Notice that this function throws a data_error exception if the last call to deflate/3 was not called with Flush set to finish.

deflateInit(Z) -> ok

Types

Same as zlib:deflateInit(Z, default).

deflateInit(Z, Level) -> ok

Types

Level = zlevel()

Initializes a zlib stream for compression.

Level decides the compression level to be used:

  • 0 (none), gives no compression
  • 1 (best_speed) gives best speed
  • 9 (best_compression) gives best compression

deflateInit(Z, Level, Method, WindowBits, MemLevel, Strategy) ->
               ok

Types

Level = zlevel()
Method = zmethod()
WindowBits = zwindowbits()
MemLevel = zmemlevel()
Strategy = zstrategy()

Initiates a zlib stream for compression.

Level

Compression level to use:

  • 0 (none), gives no compression
  • 1 (best_speed) gives best speed
  • 9 (best_compression) gives best compression
Method

Compression method to use, currently the only supported method is deflated.

WindowBits

The base two logarithm of the window size (the size of the history buffer). It is to be in the range 8 through 15. Larger values result in better compression at the expense of memory usage. Defaults to 15 if deflateInit/2 is used. A negative WindowBits value suppresses the zlib header (and checksum) from the stream. Notice that the zlib source mentions this only as a undocumented feature.

Warning

Due to a known bug in the underlying zlib library, WindowBits values 8 and -8 do not work as expected. In zlib versions before 1.2.9 values 8 and -8 are automatically changed to 9 and -9. From zlib version 1.2.9 value -8 is rejected causing zlib:deflateInit/6 to fail (8 is still changed to 9). It also seem possible that future versions of zlib may fix this bug and start accepting 8 and -8 as is.

Conclusion: Avoid values 8 and -8 unless you know your zlib version supports them.

MemLevel

Specifies how much memory is to be allocated for the internal compression state: MemLevel=1 uses minimum memory but is slow and reduces compression ratio; MemLevel=9 uses maximum memory for optimal speed. Defaults to 8.

Strategy

Tunes the compression algorithm. Use the following values:

  • default for normal data
  • filtered for data produced by a filter (or predictor)
  • huffman_only to force Huffman encoding only (no string match)
  • rle to limit match distances to one (run-length encoding)

Filtered data consists mostly of small values with a somewhat random distribution. In this case, the compression algorithm is tuned to compress them better. The effect of filtered is to force more Huffman coding and less string matching; it is somewhat intermediate between default and huffman_only. rle is designed to be almost as fast as huffman_only, but gives better compression for PNG image data.

Strategy affects only the compression ratio, but not the correctness of the compressed output even if it is not set appropriately.

deflateParams(Z, Level, Strategy) -> ok

Types

Level = zlevel()
Strategy = zstrategy()

Dynamically updates the compression level and compression strategy. The interpretation of Level and Strategy is as in deflateInit/6. This can be used to switch between compression and straight copy of the input data, or to switch to a different kind of input data requiring a different strategy. If the compression level is changed, the input available so far is compressed with the old level (and can be flushed); the new level takes effect only at the next call of deflate/3.

Before the call of deflateParams, the stream state must be set as for a call of deflate/3, as the currently available input may have to be compressed and flushed.

deflateReset(Z) -> ok

Types

Equivalent to deflateEnd/1 followed by deflateInit/1,2,6, but does not free and reallocate all the internal compression state. The stream keeps the same compression level and any other attributes.

deflateSetDictionary(Z, Dictionary) -> Adler32

Types

Dictionary = iodata()
Adler32 = integer()

Initializes the compression dictionary from the specified byte sequence without producing any compressed output.

This function must be called immediately after deflateInit/1,2,6 or deflateReset/1, before any call of deflate/3. The compressor and decompressor must use the same dictionary (see inflateSetDictionary/2).

The Adler checksum of the dictionary is returned.

getBufSize(Z) -> Size

Types

Size = integer() >= 0

Gets the size of the intermediate buffer.

gunzip(Data) -> Decompressed

Types

Data = iodata()
Decompressed = binary()

Uncompresses data with gz headers and checksum.

gzip(Data) -> Compressed

Types

Data = iodata()
Compressed = binary()

Compresses data with gz headers and checksum.

inflate(Z, Data) -> Decompressed

Types

Data = iodata()
Decompressed = iolist()

Decompresses as much data as possible. It can introduce some output latency (reading input without producing any output).

If a preset dictionary is needed at this point (see inflateSetDictionary/2), inflate/2 throws a {need_dictionary,Adler} exception, where Adler is the Adler-32 checksum of the dictionary chosen by the compressor.

inflateChunk(Z) -> Decompressed | {more, Decompressed}

Types

Decompressed = iolist()

Reads the next chunk of uncompressed data, initialized by inflateChunk/2.

This function is to be repeatedly called, while it returns {more, Decompressed}.

inflateChunk(Z, Data) -> Decompressed | {more, Decompressed}

Types

Data = iodata()
Decompressed = iolist()

Like inflate/2, but decompresses no more data than will fit in the buffer configured through setBufSize/2. Is is useful when decompressing a stream with a high compression ratio, such that a small amount of compressed input can expand up to 1000 times.

This function returns {more, Decompressed}, when there is more output available, and inflateChunk/1 is to be used to read it.

This function can introduce some output latency (reading input without producing any output).

If a preset dictionary is needed at this point (see inflateSetDictionary/2), this function throws a {need_dictionary,Adler} exception, where Adler is the Adler-32 checksum of the dictionary chosen by the compressor.

Example:

walk(Compressed, Handler) ->
    Z = zlib:open(),
    zlib:inflateInit(Z),
    % Limit single uncompressed chunk size to 512kb
    zlib:setBufSize(Z, 512 * 1024),
    loop(Z, Handler, zlib:inflateChunk(Z, Compressed)),
    zlib:inflateEnd(Z),
    zlib:close(Z).

loop(Z, Handler, {more, Uncompressed}) ->
    Handler(Uncompressed),
    loop(Z, Handler, zlib:inflateChunk(Z));
loop(Z, Handler, Uncompressed) ->
    Handler(Uncompressed).

inflateEnd(Z) -> ok

Types

Ends the inflate session and cleans all data used. Notice that this function throws a data_error exception if no end of stream was found (meaning that not all data has been uncompressed).

inflateInit(Z) -> ok

Types

Initializes a zlib stream for decompression.

inflateInit(Z, WindowBits) -> ok

Types

WindowBits = zwindowbits()

Initializes a decompression session on zlib stream.

WindowBits is the base two logarithm of the maximum window size (the size of the history buffer). It is to be in the range 8 through 15. Default to 15 if inflateInit/1 is used.

If a compressed stream with a larger window size is specified as input, inflate/2 throws the data_error exception.

A negative WindowBits value makes zlib ignore the zlib header (and checksum) from the stream. Notice that the zlib source mentions this only as a undocumented feature.

inflateReset(Z) -> ok

Types

Equivalent to inflateEnd/1 followed by inflateInit/1, but does not free and reallocate all the internal decompression state. The stream will keep attributes that could have been set by inflateInit/1,2.

inflateSetDictionary(Z, Dictionary) -> ok

Types

Dictionary = iodata()

Initializes the decompression dictionary from the specified uncompressed byte sequence. This function must be called immediately after a call of inflate/2 if this call threw a {need_dictionary,Adler} exception. The dictionary chosen by the compressor can be determined from the Adler value thrown by the call to inflate/2. The compressor and decompressor must use the same dictionary (see deflateSetDictionary/2).

Example:

unpack(Z, Compressed, Dict) ->
     case catch zlib:inflate(Z, Compressed) of
          {'EXIT',{{need_dictionary,DictID},_}} ->
                   zlib:inflateSetDictionary(Z, Dict),
                 Uncompressed = zlib:inflate(Z, []);
          Uncompressed ->
                 Uncompressed
     end.

inflateGetDictionary(Z) -> Dictionary

Types

Dictionary = iolist()

Returns the decompression dictionary currently in use by the stream. This function must be called between inflateInit/1,2 and inflateEnd.

Only supported if ERTS was compiled with zlib >= 1.2.8.

open() -> zstream()

Opens a zlib stream.

setBufSize(Z, Size) -> ok

Types

Size = integer() >= 0

Sets the intermediate buffer size.

uncompress(Data) -> Decompressed

Types

Data = iodata()
Decompressed = binary()

Uncompresses data with zlib headers and checksum.

unzip(Data) -> Decompressed

Types

Data = iodata()
Decompressed = binary()

Uncompresses data without zlib headers and checksum.

zip(Data) -> Compressed

Types

Data = iodata()
Compressed = binary()

Compresses data without zlib headers and checksum.