zlib(n)
π zlib β the manual, but make it a group chat π
okay so real talk, the man page name is zlib(n) β that little (n) is just telling you it hangs out in the Tcl Built-In Commands section of the manual. anyway. hi bestie. buckle up. π«Ά
β¨ NAME β¨
zlib β compression and decompression operations. short, sweet, iconic. π
π SYNOPSIS π
zlib subcommand arg ...
πΈ DESCRIPTION πΈ
The zlib command is your all-access pass to the compression and check-summing facilities of the Zlib library, written by Jean-loup Gailly and Mark Adler. (legends. no cap. π)
It has the following subcommands. yes, all of them. let's go. π¦
π COMPRESSION SUBCOMMANDS π
zlib compress string ?level?β Returns the zlib-format compressed binary data of the binary string instring. Iflevelis present, it gives the compression level to use, from0(which is uncompressed, like, why even) to9(maximally compressed, go off π ).zlib decompress string ?bufferSize?β Returns the uncompressed version of the raw compressed binary data instring. IfbufferSizeis present, it's 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. Iflevelis present, it gives the compression level to use, from0(uncompressed) to9(maximally compressed).zlib gunzip string ?-headerVar varName?β Returns 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?β Returns 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. (just. don't. π)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. IfbufferSizeis present, it's 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. (so likeβ¦ you're on your own for that one bestie π)-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. (glow-up behavior fr fr β¨)
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 options 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, see STREAMING INSTANCE COMMAND below. 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 unzips. You feed it gzip-format input, it hands you back the raw uncompressed bytes. It's giving decompression era πzlib stream gzip ?-header header? ?-level level?β the glow-up direction: this stream compresses, and what comes out is gzip-format output. Want to control the squeeze?-level leveltakes an integer from0to9. And if you pass-header header, that's a header descriptor dictionary β for the keys you're gonna wanna go peek atzlib gzip. πzlib stream inflate ?-dictionary bindata?β decompressing stream, raw compressed input in, uncompressed output out. If you hand itbindata, that's a compression dictionary to use. But listen β there are literally no checks in place to determine whether the compression dictionary is correct. It will not warn you. It will not save you. It'll just vibe and possibly produce nonsense. π
β¨ CHECKSUMMING SUBCOMMANDS β¨
These ones don't stream anything, they just do math on your bytes and hand you a number. Iconic.
zlib adler32 string ?initValue?β computes a checksum of the binary stringstringusing the Adler-32 algorithm. If you passinitValue, that's what seeds the checksum engine. π«Άzlib crc32 string ?initValue?β same deal but with the CRC-32 algorithm. Compute a checksum of binary stringstring;initValue, if given, initializes the checksum engine. β
π STREAMING INSTANCE COMMAND π
okay so this is the part that lowkey changes everything. Streaming compression instance commands get produced by the zlib stream command β they're little objects you hold onto. You load data in by calling their put subcommand one or more times, and you pull the transformed data back out with their get subcommand one or more times. In-and-out. Main character energy, fr fr. π¦
The full set of subcommands supported by a streaming instance command, stream, is as follows:
stream add ?option...? dataβ a lil short-cut, no cap. It's literally "stream put ?option...? data" followed by "stream get". One line, two vibes. β¨stream checksumβ returns the checksum of all the uncompressed data this stream has seen so far. πstream closeβ deletes this stream and frees up all resources associated with it. Say goodbye, be responsible, periodt. π§stream eofβ returns a boolean telling you whether the end of the stream has been reached (as determined by the compressed data itself). Not all formats support detection of the end of the stream, so don't come for it if it can't tell you. πstream finalizeβ short-cut for "stream put -finalize {}".stream flushβ short-cut for "stream put -flush {}".stream fullflushβ short-cut for "stream put -fullflush {}".stream get ?count?β returns up tocountbytes fromstream's internal buffers, with the transformation applied. Omitcountand you get the entire contents of the buffers, all of it, no crumbs. πstream headerβ returns the gzip header description dictionary extracted from the stream. Only supported for streams created with their mode parameter set togunzipβ anything else and it's simply not the moment. πstream put ?option...? dataβ appends the contents of the binary stringdatatostream's internal buffers while applying the transformation. The following options are supported (or any unambiguous prefix of them), and they change up how the transformation gets applied:-dictionary binDataβ sets the compression dictionary used when compressing or decompressing the data tobinData.-finalizeβ marks the stream as finished, making sure all bytes have been wholly compressed or decompressed. For gzip streams this also makes sure the footer gets written to the stream. After this you'll need toresetthe stream before writing more data to it β though you can still read data out of the stream with thegetsubcommand. π
This option is mutually exclusive with the-flushand-fullflushoptions. Pick one, bestie.
-flushβ makes sure a decompressor consuming the bytes this (compressing) stream is producing will be able to produce all the bytes compressed so far. There's a performance penalty for this, so it's a trade, not a freebie. β¨
This option is mutually exclusive with the-finalizeand-fullflushoptions.
-fullflushβ the extra version: not only can a decompressor handle all the bytes produced so far (like-flushabove), but it can also restart from this point if it detects the stream is partially corrupt. This incurs a substantial performance penalty. Highkey powerful, highkey expensive. π₯
This option is mutually exclusive with the-finalizeand-flushoptions.
stream resetβ puts any stream back into a state where it can process more data β including streams that have been finalized or that have reached eof. Heads up though: it throws away all internally buffered data. Gone. Poof. π«Ά
πΈ EXAMPLES πΈ
To compress a Tcl string, you first gotta convert it to a particular charset encoding, because the zlib command always operates on binary strings. That's the rule. π
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. Slay, honestly:
set strm [zlib stream compress]
$strm put [encoding convertto utf-8 $string]
# ...
$strm finalize
set compData [$strm get]
$strm close
π« SEE ALSO π«
binary(n), chan(n), encoding(n), Tcl_ZlibDeflate(3), RFC1950 - RFC1952
β KEYWORDS β
compress, decompress, deflate, gzip, inflate, zlib
and that's the whole page, bestie π this one ships as part of Tcl 8.6, filed under zlib(n) β so go forth, compress responsibly, and don't forget the dictionary check that literally does not exist. love u, bye πβ¨
π¦ where this page comes from
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- 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