3.11. Data Compression Functions

This section documents functions for compressing and uncompressing data.

3.11.1. bunzip2_to_binary()

Synopsis

Uncompresses the given data with the bzip2 algorithm and returns the uncompressed data as a binary.

Prototype

binary bunzip2_to_binary(binary $bin)

nothing bunzip2_to_binary() (RT_NOOP)

Example
$bin = bunzip2_to_binary($data);

Table 3.328. Arguments and Return Values for bunzip2_to_binary()

Argument Type

Return Type

Description

binary $bin

binary

Uncompresses the input data and returns a binary object.


Table 3.329. Exceptions Thrown by bunzip2_to_binary()

err

desc

BZIP2-DECOMPRESS-ERROR

bzlib returned an error while processing.


3.11.2. bunzip2_to_string()

Synopsis

Uncompresses the given data with the bzip2 algorithm and returns the uncompressed data as a string. An optional second string argument may be passed to give the character encoding of the string; if not present, the default character encoding for the process is assumed.

Prototype

string bunzip2_to_string(binary $bin, string $encoding = defaultEncoding)

nothing bunzip2_to_string() (RT_NOOP)

Example
$str = bunzip2_to_string($bzip2_string);

Table 3.330. Arguments and Return Values for bunzip2_to_string()

Argument Type

Return Type

Description

binary $bin, [string $encoding]

binary

Uncompresses the input data and returns a string. The optional second argument sets the string encoding of the resulting string, otherwise the default encoding is assumed.


Table 3.331. Exceptions Thrown by bunzip_to_string()

err

desc

BZIP2-DECOMPRESS-ERROR

bzlib returned an error while processing.


3.11.3. bzip2()

Synopsis

Compresses the given data with the bzip2 algorithm and returns the uncompressed data as a binary. The optional second argument specifies the compression level; if no second argument is given, a value of 9 is used, taking the most memory and proving the best compression ratio. The second argument if given must be a value from 1 - 9. Note that strings are compressed without the trailing null character.

Prototype
binary bzip2(data $bin, softint $level = 9)

nothing bzip2() (RT_NOOP)

Example
$bin = compress("hello");

Table 3.332. Arguments and Return Values for bzip2()

Argument Type

Return Type

Description

data $bin, softint $level = 9

binary

Compresses the input data and returns a binary object. The optional level argument should be an integer between 1 and 9, 9 meaning the highest compression level. If this argument is not present then a default value of 9 is assumed.


Table 3.333. Exceptions Thrown by bzip2()

err

desc

BZIP2-COMPRESS-ERROR

The bzip2 library reported an error while performing the compression.

BZLIB2-LEVEL-ERROR

The level argument was not between 1 and 9 inclusive.


3.11.4. compress()

Synopsis

Performs zlib-based "deflate" data compression (RFC 1951) and returns a binary object of the compressed data. The optional second argument specifies the compression level; if no second argument is given, then a tradeoff between speed and compression size is taken (default: Z_DEFAULT_COMPRESSION, meaning to let zlib use its default compression ratio). Note that strings are compressed without the trailing null character.

Prototype

binary compress(data $bin, softint $level = Z_DEFAULT_COMPRESSION)

nothing compress() (RT_NOOP)

Example
$bin = compress("hello");

Table 3.334. Arguments and Return Values for compress()

Argument Type

Return Type

Description

data $bin, softint $level = Z_DEFAULT_COMPRESSION

binary

Compresses the input data and returns a binary object. The optional level argument should be an integer between 1 and 9, 9 meaning the highest compression level. If this argument is not present then a default value of -1 is assumed, meaning to let zlib use its default compression ratio.


Table 3.335. Exceptions Thrown by compress()

err

desc

ZLIB-LEVEL-ERROR

the compression level is invalid (must be between 1 - 9 inclusive).

ZLIB-ERROR

zlib returned an error while processing.


3.11.5. gunzip_to_binary()

Synopsis

Uncompresses gzipped data using zlib functions and returns a binary object of the uncompressed data.

Prototype

binary gunzip_to_binary(binary $bin)

nothing gunzip_to_binary() (RT_NOOP)

Example
$bin = gunzip_to_binary($data);

Table 3.336. Arguments and Return Values for gunzip_to_binary()

Argument Type

Return Type

Description

binary $bin

binary

Uncompresses the input data and returns a binary object.


Table 3.337. Exceptions Thrown by gunzip_to_binary()

err

desc

ZLIB-ERROR

zlib returned an error while processing.


3.11.6. gunzip_to_string()

Synopsis

Uncompresses gzipped data using zlib functions and returns a string of the uncompressed data. An optional second string argument may be passed to give the character encoding of the string; if not present, the default character encoding for the process is assumed.

Prototype

string gunzip_to_string(binary $bin)

string gunzip_to_string(binary $bin, string $encoding)

nothing gunzip_to_string() (RT_NOOP)

Example
$str = gunzip_to_string($gzipped_string);

Table 3.338. Arguments and Return Values for gunzip_to_string()

Argument Type

Return Type

Description

binary $bin, [string $encoding]

binary

Uncompresses the input data and returns a string. The optional second argument sets the string encoding of the resulting string, otherwise the default encoding is assumed.


Table 3.339. Exceptions Thrown by gunzip_to_string()

err

desc

ZLIB-ERROR

zlib returned an error while processing.


3.11.7. gzip()

Synopsis

Performs zlib-based "gzip" data compression (RFC 1952) and returns a binary object of the compressed data. The optional second argument specifies the compression level between 1 (lowest comperssion level) and 9 (highest compression level); if no second argument is given, then a tradeoff between speed and compression size is taken (default: Z_DEFAULT_COMPRESSION). Note that strings are compressed without the trailing null character.

Prototype

binary gzip(string $data, softint $level = Z_DEFAULT_COMPRESSION)

nothing gzip() (RT_NOOP)

Example
$bin = gzip($data);

Table 3.340. Arguments and Return Values for gzip()

Argument Type

Return Type

Description

data $data, softint $level = Z_DEFAULT_COMPRESSION

binary

Compresses the input data and returns a binary object of the gzipped data. The optional level argument should be an integer between 1 and 9, 9 meaning the highest compression level. If this argument is not present then the default value for the zlib library is assumed.


Table 3.341. Exceptions Thrown by gzip()

err

desc

ZLIB-LEVEL-ERROR

the compression level is invalid (must be between 1 - 9 inclusive).

ZLIB-ERROR

zlib returned an error while processing.


3.11.8. uncompress_to_binary()

Synopsis

Uncompresses data using zlib functions and returns a binary object of the uncompressed data.

Prototype

binary uncompress_to_binary(binary $bin)

nothing uncompress_to_binary() (RT_NOOP)

Example
$bin = uncompress_to_binary($compressed_data);

Table 3.342. Arguments and Return Values for uncompress_to_binary()

Argument Type

Return Type

Description

binary $bin

binary

Uncompresses the input data and returns a binary object.


Table 3.343. Exceptions Thrown by uncompress_to_binary()

err

desc

ZLIB-ERROR

zlib returned an error while processing.


3.11.9. uncompress_to_string()

Synopsis

Uncompresses data using zlib functions and returns a string of the uncompressed data. An optional second string argument may be passed to give the character encoding of the string; if not present, the default character encoding for the process is assumed.

Prototype

string uncompress_to_string(binary $bin)

string uncompress_to_string(binary $bin, string $encoding)

nothing uncompress_to_string() (RT_NOOP)

Example
$str = uncompress_to_string($compressed_data);

Table 3.344. Arguments and Return Values for uncompress_to_string()

Argument Type

Return Type

Description

binary $bin, [string $encoding]

binary

Uncompresses the input data and returns a string. The optional second argument sets the string encoding of the resulting string, otherwise the default encoding is assumed.


Table 3.345. Exceptions Thrown by uncompress_to_string()

err

desc

ZLIB-ERROR

zlib returned an error while processing.