Previous section To contents Next section

14.7 Gz

The Gz module contains functions to compress and uncompress strings using the same algorithm as the program gzip. Packing can be done in streaming mode or all at once. Note that this module is only available if the gzip library was available when Pike was compiled. The Gz module consists of two classes; Gz.deflate and Gz.inflate. Gz.deflate is used to pack data and Gz.inflate is used to unpack data. (Think "inflatable boat") Note that these functions use the same algorithm as gzip, they do not use the exact same format however, so you cannot directly unzip gzipped files with these routines. Support for this will be added in the future.

CLASS
Gz.deflate - string packer

DESCRIPTION
Gz.inflate is a builtin program written in C. It interfaces the packing routines in the libz library.

NOTE
This program is only available if libz was available and found when Pike was compiled.

SEE ALSO
Gz.inflate

METHOD
Gz.deflate.create - initialize packer

SYNTAX
void create(int X)
object(Gz.deflate) Gz.deflate(int X)

DESCRIPTION
This function is called when a new Gz.deflate is created. If given, X should be a number from 0 to 9 indicating the packing / CPU ratio. Zero means no packing, 2-3 is considered 'fast', 6 is default and higher is considered 'slow' but gives better packing.

This function can also be used to re-initialize a Gz.deflate object so it can be re-used.

METHOD
Gz.deflate.deflate - pack data

SYNTAX
string deflate(string data, int flush);

DESCRIPTION
This function performs gzip style compression on a string and returns the packed data. Streaming can be done by calling this function several times and concatenating the returned data. The optional 'flush' argument should be one f the following:

Gz.NO_FLUSH Only data that doesn't fit in the internal buffers is returned.
Gz.PARTIAL_FLUSH All input is packed and returned.
Gz.SYNC_FLUSH All input is packed and returned.
Gz.FINISH All input is packed and an 'end of data' marker is appended.

Using flushing will degrade packing. Normally NO_FLUSH should be used until the end of the data when FINISH should be used. For interactive data PARTIAL_FLUSH should be used.

SEE ALSO
Gz.inflate->inflate

CLASS
Gz.inflate - string unpacker

DESCRIPTION
Gz.inflate is a builtin program written in C. It interfaces the packing routines in the libz library.

NOTE
This program is only available if libz was available and found when Pike was compiled.

SEE ALSO
Gz.deflate

METHOD
Gz.inflate.create - initialize unpacker

SYNTAX
void create()
object(Gz.inflate) Gz.inflate()

DESCRIPTION
This function is called when a new Gz.inflate is created. It can also be called after the object has been used to re-initialize it.

METHOD
Gz.inflate.inflate - unpack data

SYNTAX
string inflate(string data);

DESCRIPTION
This function performs gzip style decompression. It can inflate a whole file at once or in blocks.

EXAMPLE
import Stdio;
// whole file
write(Gz.inflate()->inflate(stdin->read());

// streaming (blocks)
function inflate=Gz.inflate()->inflate;
while(string s=stdin->read(8192))

write(inflate(s));

SEE ALSO
Gz.deflate->deflate


Previous section To contents Next section