Erlang -- zlib

User's Guide
Reference Manual
Release Notes
PDF
Top

Erlang Run-Time System Application (ERTS)
Reference Manual
Version 5.9.1

Expand All
Contract All

Table of Contents

- erl_prim_loader
  - Top of manual page
  - start/3
  - get_file/1
  - get_path/0
  - list_dir/1
  - read_file_info/1
  - set_path/1
- erlang
  - Top of manual page
  - abs/1
  - adler32/1
  - adler32/2
  - adler32_combine/3
  - append_element/2
  - apply/2
  - apply/3
  - atom_to_binary/2
  - atom_to_list/1
  - binary_part/2
  - binary_part/3
  - binary_to_atom/2
  - binary_to_existing_atom/2
  - binary_to_list/1
  - binary_to_list/3
  - bitstring_to_list/1
  - binary_to_term/1
  - binary_to_term/2
  - bit_size/1
  - bump_reductions/1
  - byte_size/1
  - cancel_timer/1
  - check_old_code/1
  - check_process_code/2
  - crc32/1
  - crc32/2
  - crc32_combine/3
  - date/0
  - decode_packet/3
  - delete_module/1
  - demonitor/1
  - demonitor/2
  - disconnect_node/1
  - display/1
  - element/2
  - erase/0
  - erase/1
  - error/1
  - error/2
  - exit/1
  - exit/2
  - external_size/1
  - external_size/2
  - float/1
  - float_to_list/1
  - fun_info/1
  - fun_info/2
  - fun_to_list/1
  - function_exported/3
  - garbage_collect/0
  - garbage_collect/1
  - get/0
  - get/1
  - get_cookie/0
  - get_keys/1
  - get_stacktrace/0
  - group_leader/0
  - group_leader/2
  - halt/0
  - halt/1
  - halt/2
  - hash/2
  - hd/1
  - hibernate/3
  - integer_to_list/1
  - integer_to_list/2
  - iolist_to_binary/1
  - iolist_size/1
  - is_alive/0
  - is_atom/1
  - is_binary/1
  - is_bitstring/1
  - is_boolean/1
  - is_builtin/3
  - is_float/1
  - is_function/1
  - is_function/2
  - is_integer/1
  - is_list/1
  - is_number/1
  - is_pid/1
  - is_port/1
  - is_process_alive/1
  - is_record/2
  - is_record/3
  - is_reference/1
  - is_tuple/1
  - length/1
  - link/1
  - list_to_atom/1
  - list_to_binary/1
  - list_to_bitstring/1
  - list_to_existing_atom/1
  - list_to_float/1
  - list_to_integer/1
  - list_to_integer/2
  - list_to_pid/1
  - list_to_tuple/1
  - load_module/2
  - load_nif/2
  - loaded/0
  - localtime/0
  - localtime_to_universaltime/1
  - localtime_to_universaltime/2
  - make_ref/0
  - make_tuple/2
  - make_tuple/3
  - max/2
  - md5/1
  - md5_final/1
  - md5_init/0
  - md5_update/2
  - memory/0
  - memory/1
  - min/2
  - module_loaded/1
  - monitor/2
  - monitor_node/2
  - monitor_node/3
  - nif_error/1
  - nif_error/2
  - node/0
  - node/1
  - nodes/0
  - nodes/1
  - now/0
  - open_port/2
  - phash/2
  - phash2/1
  - pid_to_list/1
  - port_close/1
  - port_command/2
  - port_command/3
  - port_connect/2
  - port_control/3
  - port_call/3
  - port_info/1
  - port_info/2
  - port_to_list/1
  - ports/0
  - pre_loaded/0
  - process_display/2
  - process_flag/2
  - process_flag/3
  - process_info/1
  - process_info/2
  - processes/0
  - purge_module/1
  - put/2
  - raise/3
  - read_timer/1
  - ref_to_list/1
  - register/2
  - registered/0
  - resume_process/1
  - round/1
  - self/0
  - send/2
  - send/3
  - send_after/3
  - send_nosuspend/2
  - send_nosuspend/3
  - set_cookie/2
  - setelement/3
  - size/1
  - spawn/1
  - spawn/2
  - spawn/3
  - spawn/4
  - spawn_link/1
  - spawn_link/2
  - spawn_link/3
  - spawn_link/4
  - spawn_monitor/1
  - spawn_monitor/3
  - spawn_opt/2
  - spawn_opt/3
  - spawn_opt/4
  - spawn_opt/5
  - split_binary/2
  - start_timer/3
  - statistics/1
  - suspend_process/2
  - suspend_process/1
  - system_flag/2
  - system_info/1
  - system_monitor/0
  - system_monitor/1
  - system_monitor/2
  - system_profile/0
  - system_profile/2
  - term_to_binary/1
  - term_to_binary/2
  - throw/1
  - time/0
  - tl/1
  - trace/3
  - trace_delivered/1
  - trace_info/2
  - trace_pattern/2
  - trace_pattern/3
  - trunc/1
  - tuple_size/1
  - tuple_to_list/1
  - universaltime/0
  - universaltime_to_localtime/1
  - unlink/1
  - unregister/1
  - whereis/1
  - yield/0
- init
  - Top of manual page
  - boot/1
  - get_argument/1
  - get_arguments/0
  - get_plain_arguments/0
  - get_status/0
  - reboot/0
  - restart/0
  - script_id/0
  - stop/0
  - stop/1
- zlib
  - Top of manual page
  - open/0
  - close/1
  - deflateInit/1
  - deflateInit/2
  - deflateInit/6
  - deflate/2
  - deflate/3
  - deflateSetDictionary/2
  - deflateReset/1
  - deflateParams/3
  - deflateEnd/1
  - inflateInit/1
  - inflateInit/2
  - inflate/2
  - inflateSetDictionary/2
  - inflateReset/1
  - inflateEnd/1
  - setBufSize/2
  - getBufSize/1
  - crc32/1
  - crc32/2
  - crc32/3
  - crc32_combine/4
  - adler32/2
  - adler32/3
  - adler32_combine/4
  - compress/1
  - uncompress/1
  - zip/1
  - unzip/1
  - gzip/1
  - gunzip/1
- epmd
- erl
- erlc
- werl
- escript
- erlsrv
- start_erl
- erl_set_memory_block
  - Top of manual page
  - erl_set_memory_block()
  - erl_memory_show()
  - erl_mem_info_get()
- run_erl
- start
- erl_driver
  - Top of manual page
  - driver_system_info()
  - driver_output()
  - driver_output2()
  - driver_output_binary()
  - driver_outputv()
  - driver_vec_to_buf()
  - driver_set_timer()
  - driver_cancel_timer()
  - driver_read_timer()
  - driver_get_now()
  - driver_select()
  - driver_alloc()
  - driver_realloc()
  - driver_free()
  - driver_alloc_binary()
  - driver_realloc_binary()
  - driver_free_binary()
  - driver_binary_get_refc()
  - driver_binary_inc_refc()
  - driver_binary_dec_refc()
  - driver_enq()
  - driver_pushq()
  - driver_deq()
  - driver_sizeq()
  - driver_enq_bin()
  - driver_pushq_bin()
  - driver_peekqv()
  - driver_peekq()
  - driver_enqv()
  - driver_pushqv()
  - driver_pdl_create()
  - driver_pdl_lock()
  - driver_pdl_unlock()
  - driver_pdl_get_refc()
  - driver_pdl_inc_refc()
  - driver_pdl_dec_refc()
  - driver_monitor_process()
  - driver_demonitor_process()
  - driver_get_monitored_process()
  - driver_compare_monitors()
  - add_driver_entry()
  - remove_driver_entry()
  - erl_errno_id()
  - set_busy_port()
  - set_port_control_flags()
  - driver_failure_eof()
  - driver_failure_atom()
  - driver_failure_posix()
  - driver_failure()
  - driver_connected()
  - driver_caller()
  - driver_output_term()
  - driver_mk_atom()
  - driver_mk_port()
  - driver_send_term()
  - driver_async ()
  - driver_async_cancel()
  - driver_lock_driver()
  - driver_create_port()
  - erl_drv_thread_create()
  - erl_drv_thread_opts_create()
  - erl_drv_thread_opts_destroy()
  - erl_drv_thread_exit()
  - erl_drv_thread_join()
  - erl_drv_thread_self()
  - erl_drv_equal_tids()
  - erl_drv_mutex_create()
  - erl_drv_mutex_destroy()
  - erl_drv_mutex_lock()
  - erl_drv_mutex_trylock()
  - erl_drv_mutex_unlock()
  - erl_drv_cond_create()
  - erl_drv_cond_destroy()
  - erl_drv_cond_signal()
  - erl_drv_cond_broadcast()
  - erl_drv_cond_wait()
  - erl_drv_rwlock_create()
  - erl_drv_rwlock_destroy()
  - erl_drv_rwlock_rlock()
  - erl_drv_rwlock_tryrlock()
  - erl_drv_rwlock_runlock()
  - erl_drv_rwlock_rwlock()
  - erl_drv_rwlock_tryrwlock()
  - erl_drv_rwlock_rwunlock()
  - erl_drv_tsd_key_create()
  - erl_drv_tsd_key_destroy()
  - erl_drv_tsd_set()
  - erl_drv_tsd_get()
  - erl_drv_putenv()
  - erl_drv_getenv()
- driver_entry
- erts_alloc
- erl_nif
  - Top of manual page
  - enif_alloc()
  - enif_alloc_binary()
  - enif_alloc_env()
  - enif_alloc_resource()
  - enif_clear_env()
  - enif_compare()
  - enif_cond_broadcast()
  - enif_cond_create()
  - enif_cond_destroy()
  - enif_cond_signal()
  - enif_cond_wait()
  - enif_equal_tids()
  - enif_free()
  - enif_free_env()
  - enif_get_atom()
  - enif_get_atom_length()
  - enif_get_double()
  - enif_get_int()
  - enif_get_int64()
  - enif_get_local_pid()
  - enif_get_list_cell()
  - enif_get_list_length()
  - enif_get_long()
  - enif_get_resource()
  - enif_get_string()
  - enif_get_tuple()
  - enif_get_uint()
  - enif_get_uint64()
  - enif_get_ulong()
  - enif_inspect_binary()
  - enif_inspect_iolist_as_binary()
  - enif_is_atom()
  - enif_is_binary()
  - enif_is_empty_list()
  - enif_is_exception()
  - enif_is_number()
  - enif_is_fun()
  - enif_is_identical()
  - enif_is_pid()
  - enif_is_port()
  - enif_is_ref()
  - enif_is_tuple()
  - enif_is_list()
  - enif_keep_resource()
  - enif_make_atom()
  - enif_make_atom_len()
  - enif_make_badarg()
  - enif_make_binary()
  - enif_make_copy()
  - enif_make_double()
  - enif_make_existing_atom()
  - enif_make_existing_atom_len()
  - enif_make_int()
  - enif_make_int64()
  - enif_make_list()
  - enif_make_list1()
  - enif_make_list2()
  - enif_make_list3()
  - enif_make_list4()
  - enif_make_list5()
  - enif_make_list6()
  - enif_make_list7()
  - enif_make_list8()
  - enif_make_list9()
  - enif_make_list_cell()
  - enif_make_list_from_array()
  - enif_make_reverse_list()
  - enif_make_long()
  - enif_make_new_binary()
  - enif_make_pid()
  - enif_make_ref()
  - enif_make_resource()
  - enif_make_resource_binary()
  - enif_make_string()
  - enif_make_string_len()
  - enif_make_sub_binary()
  - enif_make_tuple()
  - enif_make_tuple1()
  - enif_make_tuple2()
  - enif_make_tuple3()
  - enif_make_tuple4()
  - enif_make_tuple5()
  - enif_make_tuple6()
  - enif_make_tuple7()
  - enif_make_tuple8()
  - enif_make_tuple9()
  - enif_make_tuple_from_array()
  - enif_make_uint()
  - enif_make_uint64()
  - enif_make_ulong()
  - enif_mutex_create()
  - enif_mutex_destroy()
  - enif_mutex_lock()
  - enif_mutex_trylock()
  - enif_mutex_unlock()
  - enif_open_resource_type()
  - enif_priv_data()
  - enif_realloc_binary()
  - enif_release_binary()
  - enif_release_resource()
  - enif_rwlock_create()
  - enif_rwlock_destroy()
  - enif_rwlock_rlock()
  - enif_rwlock_runlock()
  - enif_rwlock_rwlock()
  - enif_rwlock_rwunlock()
  - enif_rwlock_tryrlock()
  - enif_rwlock_tryrwlock()
  - enif_self()
  - enif_send()
  - enif_sizeof_resource()
  - enif_system_info()
  - enif_thread_create()
  - enif_thread_exit()
  - enif_thread_join()
  - enif_thread_opts_create()
  - enif_thread_opts_destroy()
  - enif_thread_self()
  - enif_tsd_key_create()
  - enif_tsd_key_destroy()
  - enif_tsd_get()
  - enif_tsd_set()

# zlib

### MODULE

zlib

### MODULE SUMMARY

Zlib Compression interface.

### DESCRIPTION

The zlib module provides an API for the zlib library (http://www.zlib.org). It is used to compress and decompress data. The data format is described by RFCs 1950 to 1952.

A typical (compress) usage looks like:

    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}}, might be thrown, where Reason describes the error. Typical reasons are:

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

zwindowbits() = -15..-9 | 9..47

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

### EXPORTS

open() -> zstream()

Open a zlib stream.

close(Z) -> ok

Types:

Z = zstream()

Closes the stream referenced by Z.

deflateInit(Z) -> ok

Types:

Z = zstream()

Same as zlib:deflateInit(Z, default).

deflateInit(Z, Level) -> ok

Types:

Z = zstream()
Level = zlevel()

Initialize a zlib stream for compression.

Level decides the compression level to be used, 0 (none), gives no compression at all, 1 (best_speed) gives best speed and 9 (best_compression) gives best compression.

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

Types:

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

Initiates a zlib stream for compression.

The Level parameter decides the compression level to be used, 0 (none), gives no compression at all, 1 (best_speed) gives best speed and 9 (best_compression) gives best compression.

The Method parameter decides which compression method to use, currently the only supported method is deflated.

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

The MemLevel parameter specifies how much memory should 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. The default value is 8.

The Strategy parameter is used to tune the compression algorithm. Use the value default for normal data, filtered for data produced by a filter (or predictor), or huffman_only to force Huffman encoding only (no string match). 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 filteredis to force more Huffman coding and less string matching; it is somewhat intermediate between default and huffman_only. The Strategy parameter only affects the compression ratio but not the correctness of the compressed output even if it is not set appropriately.

deflate(Z, Data) -> Compressed

Types:

Z = zstream()
Data = iodata()
Compressed = iolist()

Same as deflate(Z, Data, none).

deflate(Z, Data, Flush) -> Compressed

Types:

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

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

If the parameter 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 may degrade compression for some compression algorithms and so it should be used 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 the parameter 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.

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

deflateSetDictionary(Z, Dictionary) -> Adler32

Types:

Z = zstream()
Dictionary = iodata()
Adler32 = integer()

Initializes the compression dictionary from the given 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 exactly the same dictionary (see inflateSetDictionary/2). The adler checksum of the dictionary is returned.

deflateReset(Z) -> ok

Types:

Z = zstream()

This function is equivalent to deflateEnd/1 followed by deflateInit/[1|2|6], but does not free and reallocate all the internal compression state. The stream will keep the same compression level and any other attributes.

deflateParams(Z, Level, Strategy) -> ok

Types:

Z = zstream()
Level = zlevel()
Strategy = zstrategy()

Dynamically update 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 may be flushed); the new level will take 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, since the currently available input may have to be compressed and flushed.

deflateEnd(Z) -> ok

Types:

Z = zstream()

End the deflate session and cleans all data used. Note that this function will throw an data_error exception if the last call to deflate/3 was not called with Flush set to finish.

inflateInit(Z) -> ok

Types:

Z = zstream()

Initialize a zlib stream for decompression.

inflateInit(Z, WindowBits) -> ok

Types:

Z = zstream()
WindowBits = zwindowbits()

Initialize decompression session on zlib stream.

The WindowBits parameter is the base two logarithm of the maximum window size (the size of the history buffer). It should be in the range 9 through 15. The default value is 15 if inflateInit/1 is used. If a compressed stream with a larger window size is given as input, inflate() will throw the data_error exception. A negative WindowBits value makes zlib ignore the zlib header (and checksum) from the stream. Note that the zlib source mentions this only as a undocumented feature.

inflate(Z, Data) -> Decompressed

Types:

Z = zstream()
Data = iodata()
Decompressed = iolist()

inflate/2 decompresses as much data as possible. It may some introduce some output latency (reading input without producing any output).

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

inflateSetDictionary(Z, Dictionary) -> ok

Types:

Z = zstream()
Dictionary = iodata()

Initializes the decompression dictionary from the given 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 exactly 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.

inflateReset(Z) -> ok

Types:

Z = zstream()

This function is 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 may have been set by inflateInit/[1|2].

inflateEnd(Z) -> ok

Types:

Z = zstream()

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

setBufSize(Z, Size) -> ok

Types:

Z = zstream()
Size = integer() >= 0

Sets the intermediate buffer size.

getBufSize(Z) -> Size

Types:

Z = zstream()
Size = integer() >= 0

Get the size of intermediate buffer.

crc32(Z) -> CRC

Types:

Z = zstream()
CRC = integer()

Get the current calculated CRC checksum.

crc32(Z, Data) -> CRC

Types:

Z = zstream()
Data = iodata()
CRC = integer()

Calculate the CRC checksum for Data.

crc32(Z, PrevCRC, Data) -> CRC

Types:

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

Update 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.

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

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

Types:

Z = zstream()
CRC = CRC1 = CRC2 = Size2 = integer()

Combine 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. crc32_combine/4 returns the CRC checksum of [Data1,Data2], requiring only CRC1, CRC2, and Size2.

adler32(Z, Data) -> CheckSum

Types:

Z = zstream()
Data = iodata()
CheckSum = integer()

Calculate the Adler-32 checksum for Data.

adler32(Z, PrevAdler, Data) -> CheckSum

Types:

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

Update 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.

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

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

Types:

Z = zstream()
Adler = Adler1 = Adler2 = Size2 = integer()

Combine 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. adler32_combine/4 returns the Adler checksum of [Data1,Data2], requiring only Adler1, Adler2, and Size2.

compress(Data) -> Compressed

Types:

Data = iodata()
Compressed = binary()

Compress data (with zlib headers and checksum).

uncompress(Data) -> Decompressed

Types:

Data = iodata()
Decompressed = binary()

Uncompress data (with zlib headers and checksum).

zip(Data) -> Compressed

Types:

Data = iodata()
Compressed = binary()

Compress data (without zlib headers and checksum).

unzip(Data) -> Decompressed

Types:

Data = iodata()
Decompressed = binary()

Uncompress data (without zlib headers and checksum).

gzip(Data) -> Compressed

Types:

Data = iodata()
Compressed = binary()

Compress data (with gz headers and checksum).

gunzip(Data) -> Decompressed

Types:

Data = iodata()
Decompressed = binary()

Uncompress data (with gz headers and checksum).