zlib(3tcl)
π NAME π
zlib β compression and decompression operations
okay so this is zlib(3tcl), a Tcl Built-In Command page, and it's literally the manual for squishing and unsquishing data. no cap. it's giving compression royalty. π
β¨ SYNOPSIS β¨
zlib subcommand arg ...
π¦ DESCRIPTION π¦
The zlib 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 string ?level?β Returns the zlib-format compressed binary data of the binary string instring. If present,levelgives the compression level to use (from0, which is uncompressed, to9, maximally compressed). slay.zlib decompress string ?bufferSize?β Returns the uncompressed version of the raw compressed binary data instring. If present,bufferSizeis a hint as to what size of buffer is to be used to receive the data.zlib deflate string ?level?β Returns the raw compressed binary data of the binary string instring. If present,levelgives the compression level to use (from0, which is uncompressed, to9, maximally compressed).zlib gunzip string ?-headerVar varName?β Return the uncompressed contents of binary stringstring, which must have been in gzip format. If-headerVaris given, store a dictionary describing the contents of the gzip header in the variable calledvarName. 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 theQWunknown 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 thefilenamefield was modified. Suitable for use withclock format.typeβ The type of the uncompressed data (binaryortext) if known.
zlib gzip string ?-level level? ?-header dict?β Return the compressed contents of binary stringstringin gzip format. If-levelis given,levelgives the compression level to use (from0, which is uncompressed, to9, maximally compressed). If-headeris given,dictis 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 thegzipprogram, a header CRC should not 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 thefilenamekey was last modified. This will be in the same as is returned byclock secondsorfile mtime.typeβ The type of the data being compressed, beingbinaryortext.
zlib inflate string ?bufferSize?β Returns the uncompressed version of the raw compressed binary data instring. If present,bufferSizeis a hint as to what size of buffer is to be used to receive the data.
π CHANNEL SUBCOMMAND π
zlib push mode channel ?options ...?β Pushes a compressing or decompressing transformation onto the channelchannel. The transformation can be removed again withchan pop. Themodeargument determines what type of transformation is pushed; the following are supported:compressβ The transformation will be a compressing transformation that produces zlib-format data onchannel, which must be writable.decompressβ The transformation will be a decompressing transformation that reads zlib-format data fromchannel, which must be readable.deflateβ The transformation will be a compressing transformation that produces raw compressed data onchannel, which must be writable.gunzipβ The transformation will be a decompressing transformation that reads gzip-format data fromchannel, which must be readable.gzipβ The transformation will be a compressing transformation that produces gzip-format data onchannel, which must be writable.inflateβ The transformation will be a decompressing transformation that reads raw compressed data fromchannel, which must be readable.
The following options may be set when creating a transformation via the βoptions ...β to the zlib push command:
-dictionary binDataβ Sets the compression dictionary to use when working with compressing or decompressing the data to bebinData. 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 dictionaryβ Passes a description of the gzip header to create, in the same format thatzlib gzipunderstands.-level compressionLevelβ How hard to compress the data. Must be an integer from0(uncompressed) to9(maximally compressed).-limit readaheadLimitβ The maximum number of bytes ahead to read when decompressing.
This option has become irrelevant. 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 chan configure. The options are:
-checksum checksumβ 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 rawinflateanddeflateformats. The compression algorithm depends on what format is being produced or consumed.-dictionary binDataβ This read-write option gets or sets the initial compression dictionary to use when working with compressing or decompressing the data to bebinData. 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 typeβ This write-only operation flushes the current state of the compressor to the underlying channel. It is only valid for compressing transformations. Thetypemust be eithersyncorfullfor 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 dictionaryβ 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 readaheadLimitβ 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 mode ?options?β Creates a streaming compression or decompression command based on themode, and return the name of the command. For a description of how that command works, seeSTREAMING INSTANCE COMMANDbelow. The following modes and options are supported:zlib stream compress ?-dictionary bindata? ?-level level?β The stream will be a compressing stream that produces zlib-format output, using compression levellevel(if specified) which will be an integer from0to9, and the compression dictionarybindata(if specified).zlib stream decompress ?-dictionary bindata?β The stream will be a decompressing stream that takes zlib-format input and produces uncompressed output. Ifbindatais supplied, it is a compression dictionary to use if required.zlib stream deflate ?-dictionary bindata? ?-level level?β The stream will be a compressing stream that produces raw output, using compression levellevel(if specified) which will be an integer from0to9, and the compression dictionarybindata(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β bestie, this one gives you a decompressing stream that eats gzip-format input and serves uncompressed output. It's giving unzip energy β¨zlib stream gzip ?-header header? ?-level level?β this is the compressing stream that produces gzip-format output. You can pass-level level(an integer from0to9, if you specified it) and-header header(a header descriptor dictionary, if you specified it; for the keys, seezlib gzip). Slay πzlib stream inflate ?-dictionary bindata?β a decompressing stream that takes raw compressed input and gives you uncompressed output. Ifbindatais supplied, it's the compression dictionary to use. And bestie, listen: there are literally no checks in place to determine whether the compression dictionary is correct π so if it's wrong, that's a vibe check you're failing all by yourself. No cap.
β¨ CHECKSUMMING SUBCOMMANDS β¨
zlib adler32 string ?initValue?β compute a checksum of the binary stringstringusing the Adler-32 algorithm. If you passinitValue, it's used to initialize the checksum engine. β¨zlib crc32 string ?initValue?β compute a checksum of the binary stringstringusing the CRC-32 algorithm. If you passinitValue, it's used to initialize the checksum engine. π
β¨ STREAMING INSTANCE COMMAND β¨
Streaming compression instance commands are produced by the zlib stream command. You use them by calling their put subcommand one or more times to load data in, and their get subcommand one or more times to extract the transformed data. It's giving pipeline realness π¦
The full set of subcommands supported by a streaming instance command, stream, is as follows:
stream add ?option...? dataβ a short-cut for βstream put ?option...? dataβ followed by βstream getβ. Obsessed with efficiency πstream checksumβ returns the checksum of the uncompressed data seen so far by this stream. π§stream closeβ deletes this stream and frees up all resources associated with it. Bye bestie, it's giving closure β¨stream eofβ 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. So sometimes it's like, eof? maybe? πstream finalizeβ a short-cut for βstream put -finalize {}β. βstream flushβ a short-cut for βstream put -flush {}β. π½stream fullflushβ a short-cut for βstream put -fullflush {}β. π½β¨stream get ?count?β return up tocountbytes fromstream's internal buffers with the transformation applied. Ifcountis omitted, the entire contents of the buffers are returned. Gimme the bytes, all of them πstream headerβ return the gzip header description dictionary extracted from the stream. Only supported for streams created with their mode parameter set togunzip. Iykyk πstream put ?option...? dataβ append the contents of the binary stringdatatostream's internal buffers while applying the transformation. The following options are supported (or an unambiguous prefix of them), which are used to modify the way in which the transformation is applied:-dictionary binDataβ sets the compression dictionary to use when working with compressing or decompressing the data to bebinData. π-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 thegetsubcommand.
This option is mutually exclusive with the-flushand-fullflushoptions. Periodt. π
-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-finalizeand-fullflushoptions. π«Ά
-fullflushβ ensure that not only can a decompressor handle all the bytes produced so far (as with-flushabove) 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-finalizeand-flushoptions. No cap, this one costs you. π
stream resetβ 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. It's giving clean slate energy β¨
β¨ EXAMPLES β¨
To compress a Tcl string, it should be first converted to a particular charset encoding since the zlib command always operates on binary strings. Like, the encoding pipeline is not optional bestie.
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. Main character energy fr fr π
set strm [zlib stream compress]
$strm put [encoding convertto utf-8 $string]
# ...
$strm finalize
set compData [$strm get]
$strm close
β¨ SEE ALSO β¨
β¨ KEYWORDS β¨
compressdecompressdeflategzipinflatezlib
That's the whole zlib(3tcl) page, bestie β source/version: Tcl 8.6, manual page zlib(3tcl). Stay compressed, stay iconic πβ¨π¦
π¦ where this page comes from
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2008-2012 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms