Skip to content
Tcl Source Code Artifact [6c586719b2]
Login

Artifact [6c586719b2]

Artifact 6c586719b296f53c31ed3e76971bdb345c444b3ae1f9cb97e83d45f6de769936:



CommandName: zlib ManualSection: n Version: 8.6 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

zlib - compression and decompression operations

Synopsis

::: {.synopsis} ::: [zlib]{.cmd} [subcommand]{.arg} [arg]{.arg} [...]{.arg} :::

Description

The fBzlibfR command provides access to the compression and check-summing facilities of the Zlib library by Jean-loup Gailly and Mark Adler. It has the following subcommands.

Compression subcommands

zlib compress fIstringfR ?fIlevelfR? : Returns the zlib-format compressed binary data of the binary string in fIstringfR. If present, fIlevelfR gives the compression level to use (from 0, which is uncompressed, to 9, maximally compressed).

zlib decompress fIstringfR ?fIbufferSizefR? : Returns the uncompressed version of the raw compressed binary data in fIstringfR. If present, fIbufferSizefR is a hint as to what size of buffer is to be used to receive the data.

zlib deflate fIstringfR ?fIlevelfR? : Returns the raw compressed binary data of the binary string in fIstringfR. If present, fIlevelfR gives the compression level to use (from 0, which is uncompressed, to 9, maximally compressed).

zlib gunzip fIstringfR ?fB-headerVarfR fIvarNamefR? : Return the uncompressed contents of binary string fIstringfR, which must have been in gzip format. If fB-headerVarfR is given, store a dictionary describing the contents of the gzip header in the variable called fIvarNamefR. The keys of the dictionary that may be present are:

comment : The comment field from the header, if present.

crc : A boolean value describing whether a CRC of the header is computed.

filename : The filename field from the header, if present.

os : The operating system type code field from the header (if not the QW unknown value). See RFC 1952 for the meaning of these codes.

size : The size of the uncompressed data.

time : The time field from the header if non-zero, expected to be time that the file named by the fBfilenamefR field was modified. Suitable for use with fBclock formatfR.

type : The type of the uncompressed data (fBbinaryfR or fBtextfR) if known.

zlib gzip fIstringfR ?fB-levelfR fIlevelfR? ?fB-headerfR fIdictfR? : Return the compressed contents of binary string fIstringfR in gzip format. If fB-levelfR is given, fIlevelfR gives the compression level to use (from 0, which is uncompressed, to 9, maximally compressed). If fB-headerfR is given, fIdictfR is a dictionary containing values used for the gzip header. The following keys may be defined:

comment : Add the given comment to the header of the gzip-format data.

crc : A boolean saying whether to compute a CRC of the header. Note that if the data is to be interchanged with the fBgzipfR program, a header CRC should fInotfR be computed.

filename : The name of the file that the data to be compressed came from.

os : The operating system type code, which should be one of the values described in RFC 1952.

time : The time that the file named in the fBfilenamefR key was last modified. This will be in the same as is returned by fBclock secondsfR or fBfile mtimefR.

type : The type of the data being compressed, being fBbinaryfR or fBtextfR.

zlib inflate fIstringfR ?fIbufferSizefR? : Returns the uncompressed version of the raw compressed binary data in fIstringfR. If present, fIbufferSizefR is a hint as to what size of buffer is to be used to receive the data.

Channel subcommand

zlib push fImode channelfR ?fIoptions ...fR? : Pushes a compressing or decompressing transformation onto the channel fIchannelfR. The transformation can be removed again with fBchan popfR. The fImodefR argument determines what type of transformation is pushed; the following are supported:

compress : The transformation will be a compressing transformation that produces zlib-format data on fIchannelfR, which must be writable.

decompress : The transformation will be a decompressing transformation that reads zlib-format data from fIchannelfR, which must be readable.

deflate : The transformation will be a compressing transformation that produces raw compressed data on fIchannelfR, which must be writable.

gunzip : The transformation will be a decompressing transformation that reads gzip-format data from fIchannelfR, which must be readable.

gzip : The transformation will be a compressing transformation that produces gzip-format data on fIchannelfR, which must be writable.

inflate : The transformation will be a decompressing transformation that reads raw compressed data from fIchannelfR, which must be readable.

The following options may be set when creating a transformation via the "*options ...*" to the \fBzlib push\fR command:

-dictionary fIbinDatafR : Sets the compression dictionary to use when working with compressing or decompressing the data to be fIbinDatafR. Not valid for transformations that work with gzip-format data. The dictionary should consist of strings (byte sequences) that are likely to be encountered later in the data to be compressed, with the most commonly used strings preferably put towards the end of the dictionary. Tcl provides no mechanism for choosing a good such dictionary for a particular data sequence.

-header fIdictionaryfR : Passes a description of the gzip header to create, in the same format that fBzlib gzipfR understands.

-level fIcompressionLevelfR : How hard to compress the data. Must be an integer from 0 (uncompressed) to 9 (maximally compressed).

-limit fIreadaheadLimitfR : The maximum number of bytes ahead to read when decompressing.

This option has become \fBirrelevant\fR. It was originally introduced to prevent Tcl from reading beyond the end of a compressed stream in multi-stream channels to ensure that the data after was left alone for further reading, at the cost of speed.
Tcl now automatically returns any bytes it has read beyond the end of a compressed stream back to the channel, making them appear as unread to further readers.

Both compressing and decompressing channel transformations add extra configuration options that may be accessed through fBchan configurefR. The options are:

-checksum fIchecksumfR : This read-only option gets the current checksum for the uncompressed data that the compression engine has seen so far. It is valid for both compressing and decompressing transforms, but not for the raw inflate and deflate formats. The compression algorithm depends on what format is being produced or consumed.

-dictionary fIbinDatafR : This read-write options gets or sets the initial compression dictionary to use when working with compressing or decompressing the data to be fIbinDatafR. It is not valid for transformations that work with gzip-format data, and should not normally be set on compressing transformations other than at the point where the transformation is stacked. Note that this cannot be used to get the current active compression dictionary mid-stream, as that information is not exposed by the underlying library.

-flush fItypefR : This write-only operation flushes the current state of the compressor to the underlying channel. It is only valid for compressing transformations. The fItypefR must be either fBsyncfR or fBfullfR for a normal flush or an expensive flush respectively. Flushing degrades the compression ratio, but makes it easier for a decompressor to recover more of the file in the case of data corruption.

-header fIdictionaryfR : This read-only option, only valid for decompressing transforms that are processing gzip-format data, returns the dictionary describing the header read off the data stream.

-limit fIreadaheadLimitfR : This read-write option is used by decompressing channels to control the maximum number of bytes ahead to read from the underlying data source. See above for more information.

Streaming subcommand

zlib stream fImodefR ?fIoptionsfR? : Creates a streaming compression or decompression command based on the fImodefR, and return the name of the command. For a description of how that command works, see fBSTREAMING INSTANCE COMMANDfR below. The following modes and fIoptionsfR are supported:

zlib stream compress ?fB-dictionaryfR fIbindatafR? ?fB-levelfR fIlevelfR? : The stream will be a compressing stream that produces zlib-format output, using compression level fIlevelfR (if specified) which will be an integer from 0 to 9, and the compression dictionary fIbindatafR (if specified).

zlib stream decompress ?fB-dictionaryfR fIbindatafR? : The stream will be a decompressing stream that takes zlib-format input and produces uncompressed output. If fIbindatafR is supplied, it is a compression dictionary to use if required.

zlib stream deflate ?fB-dictionaryfR fIbindatafR? ?fB-levelfR fIlevelfR? : The stream will be a compressing stream that produces raw output, using compression level fIlevelfR (if specified) which will be an integer from 0 to 9, and the compression dictionary fIbindatafR (if specified). Note that the raw compressed data includes no metadata about what compression dictionary was used, if any; that is a feature of the zlib-format data.

zlib stream gunzip : The stream will be a decompressing stream that takes gzip-format input and produces uncompressed output.

zlib stream gzip ?fB-headerfR fIheaderfR? ?fB-levelfR fIlevelfR? : The stream will be a compressing stream that produces gzip-format output, using compression level fIlevelfR (if specified) which will be an integer from 0 to 9, and the header descriptor dictionary fIheaderfR (if specified; for keys see fBzlib gzipfR).

zlib stream inflate ?fB-dictionaryfR fIbindatafR? : The stream will be a decompressing stream that takes raw compressed input and produces uncompressed output. If fIbindatafR is supplied, it is a compression dictionary to use. Note that there are no checks in place to determine whether the compression dictionary is correct.

Checksumming subcommands

zlib adler32 fIstringfR ?fIinitValuefR? : Compute a checksum of binary string fIstringfR using the Adler-32 algorithm. If given, fIinitValuefR is used to initialize the checksum engine.

zlib crc32 fIstringfR ?fIinitValuefR? : Compute a checksum of binary string fIstringfR using the CRC-32 algorithm. If given, fIinitValuefR is used to initialize the checksum engine.

Streaming instance command

Streaming compression instance commands are produced by the fBzlib streamfR command. They are used by calling their fBputfR subcommand one or more times to load data in, and their fBgetfR subcommand one or more times to extract the transformed data.

The full set of subcommands supported by a streaming instance command, fIstreamfR, is as follows:

stream fBaddfR ?fIoption...fR? fIdatafR : A short-cut for "stream fBputfR ?fIoption...fR? fIdatafR" followed by "stream fBgetfR".

stream fBchecksumfR : Returns the checksum of the uncompressed data seen so far by this stream.

stream fBclosefR : Deletes this stream and frees up all resources associated with it.

stream fBeoffR : Returns a boolean indicating whether the end of the stream (as determined by the compressed data itself) has been reached. Not all formats support detection of the end of the stream.

stream fBfinalizefR : A short-cut for "stream fBput -finalize {}fR".

stream fBflushfR : A short-cut for "stream fBput -flush {}fR".

stream fBfullflushfR : A short-cut for "stream fBput -fullflush {}fR".

stream fBgetfR ?fIcountfR? : Return up to fIcountfR bytes from fIstreamfR's internal buffers with the transformation applied. If fIcountfR is omitted, the entire contents of the buffers are returned.

stream fBheaderfR : Return the gzip header description dictionary extracted from the stream. Only supported for streams created with their fImodefR parameter set to fBgunzipfR.

stream fBputfR ?fIoption...fR? fIdatafR : Append the contents of the binary string fIdatafR to fIstreamfR's internal buffers while applying the transformation. The following fIoptionfRs are supported (or an unambiguous prefix of them), which are used to modify the way in which the transformation is applied:

-dictionary fIbinDatafR : Sets the compression dictionary to use when working with compressing or decompressing the data to be fIbinDatafR.

-finalize : Mark the stream as finished, ensuring that all bytes have been wholly compressed or decompressed. For gzip streams, this also ensures that the footer is written to the stream. The stream will need to be reset before having more data written to it after this, though data can still be read out of the stream with the fBgetfR subcommand.

This option is mutually exclusive with the \fB-flush\fR and \fB-fullflush\fR options.

-flush : Ensure that a decompressor consuming the bytes that the current (compressing) stream is producing will be able to produce all the bytes that have been compressed so far, at some performance penalty. This option is mutually exclusive with the fB-finalizefR and fB-fullflushfR options.

-fullflush : Ensure that not only can a decompressor handle all the bytes produced so far (as with fB-flushfR above) but also that it can restart from this point if it detects that the stream is partially corrupt. This incurs a substantial performance penalty. This option is mutually exclusive with the fB-finalizefR and fB-flushfR options.

stream fBresetfR : Puts any stream, including those that have been finalized or that have reached eof, back into a state where it can process more data. Throws away all internally buffered data.

Examples

To compress a Tcl string, it should be first converted to a particular charset encoding since the fBzlibfR command always operates on binary strings.

set binData [encoding convertto utf-8 $string]
set compData [zlib compress $binData]

When converting back, it is also important to reverse the charset encoding:

set binData [zlib decompress $compData]
set string [encoding convertfrom utf-8 $binData]

The compression operation from above can also be done with streams, which is especially helpful when you want to accumulate the data by stages:

set strm [zlib stream compress]
$strm put [encoding convertto utf-8 $string]
# ...
$strm finalize
set compData [$strm get]
$strm close