Erlang Run-Time System Application (ERTS)

Reference Manual

Version 10.1

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.
not_initialized
The stream hasn't been initialized, eg. if inflateInit/1 wasn't called prior to a call to inflate/2.
not_on_controlling_process
The stream was used by a process that doesn't control it. Use set_controlling_process/2 if you need to transfer a stream to a different process.
data_error
The data contains errors.
stream_error
Inconsistent stream state.
{need_dictionary,Adler32}
See inflate/2.

Data Types

zstream() = reference()

A zlib stream, see open/0.

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

zflush() = none | sync | full | finish
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() >= 0

Calculates the Adler-32 checksum for Data.

Warning

This function is deprecated and will be removed in a future release. Use erlang:adler32/1 instead.

adler32(Z, PrevAdler, Data) -> CheckSum

Types

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

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)
Warning

This function is deprecated and will be removed in a future release. Use erlang:adler32/2 instead.

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

Types

Adler = Adler1 = Adler2 = Size2 = integer() >= 0

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.

Warning

This function is deprecated and will be removed in a future release. Use erlang:adler32_combine/3 instead.

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() >= 0

Gets the current calculated CRC checksum.

Warning

This function is deprecated and will be removed in a future release. Use erlang:crc32/1 on the uncompressed data instead.