1zlib(n) Tcl Built-In Commands zlib(n)
2
3
4
5______________________________________________________________________________
6
8 zlib - compression and decompression operations
9
11 zlib subcommand arg ...
12______________________________________________________________________________
13
15 The zlib command provides access to the compression and check-summing
16 facilities of the Zlib library by Jean-loup Gailly and Mark Adler. It
17 has the following subcommands.
18
19 COMPRESSION SUBCOMMANDS
20 zlib compress string ?level?
21 Returns the zlib-format compressed binary data of the binary
22 string in string. If present, level gives the compression level
23 to use (from 0, which is uncompressed, to 9, maximally com‐
24 pressed).
25
26 zlib decompress string ?bufferSize?
27 Returns the uncompressed version of the raw compressed binary
28 data in string. If present, bufferSize is a hint as to what size
29 of buffer is to be used to receive the data.
30
31 zlib deflate string ?level?
32 Returns the raw compressed binary data of the binary string in
33 string. If present, level gives the compression level to use
34 (from 0, which is uncompressed, to 9, maximally compressed).
35
36 zlib gunzip string ?-headerVar varName?
37 Return the uncompressed contents of binary string string, which
38 must have been in gzip format. If -headerVar is given, store a
39 dictionary describing the contents of the gzip header in the
40 variable called varName. The keys of the dictionary that may be
41 present are:
42
43 comment
44 The comment field from the header, if present.
45
46 crc A boolean value describing whether a CRC of the header is
47 computed.
48
49 filename
50 The filename field from the header, if present.
51
52 os The operating system type code field from the header (if
53 not the QW unknown value). See RFC 1952 for the meaning
54 of these codes.
55
56 size The size of the uncompressed data.
57
58 time The time field from the header if non-zero, expected to
59 be time that the file named by the filename field was
60 modified. Suitable for use with clock format.
61
62 type The type of the uncompressed data (binary or text) if
63 known.
64
65 zlib gzip string ?-level level? ?-header dict?
66 Return the compressed contents of binary string string in gzip
67 format. If -level is given, level gives the compression level
68 to use (from 0, which is uncompressed, to 9, maximally com‐
69 pressed). If -header is given, dict is a dictionary containing
70 values used for the gzip header. The following keys may be
71 defined:
72
73 comment
74 Add the given comment to the header of the gzip-format
75 data.
76
77 crc A boolean saying whether to compute a CRC of the header.
78 Note that if the data is to be interchanged with the gzip
79 program, a header CRC should not be computed.
80
81 filename
82 The name of the file that the data to be compressed came
83 from.
84
85 os The operating system type code, which should be one of
86 the values described in RFC 1952.
87
88 time The time that the file named in the filename key was last
89 modified. This will be in the same as is returned by
90 clock seconds or file mtime.
91
92 type The type of the data being compressed, being binary or
93 text.
94
95 zlib inflate string ?bufferSize?
96 Returns the uncompressed version of the raw compressed binary
97 data in string. If present, bufferSize is a hint as to what size
98 of buffer is to be used to receive the data.
99
100 CHANNEL SUBCOMMAND
101 zlib push mode channel ?options ...?
102 Pushes a compressing or decompressing transformation onto the
103 channel channel. The transformation can be removed again with
104 chan pop. The mode argument determines what type of transforma‐
105 tion is pushed; the following are supported:
106
107 compress
108 The transformation will be a compressing transformation
109 that produces zlib-format data on channel, which must be
110 writable.
111
112 decompress
113 The transformation will be a decompressing transformation
114 that reads zlib-format data from channel, which must be
115 readable.
116
117 deflate
118 The transformation will be a compressing transformation
119 that produces raw compressed data on channel, which must
120 be writable.
121
122 gunzip The transformation will be a decompressing transformation
123 that reads gzip-format data from channel, which must be
124 readable.
125
126 gzip The transformation will be a compressing transformation
127 that produces gzip-format data on channel, which must be
128 writable.
129
130 inflate
131 The transformation will be a decompressing transformation
132 that reads raw compressed data from channel, which must
133 be readable.
134
135 The following options may be set when creating a transformation
136 via the “options ...” to the zlib push command:
137
138 -dictionary binData
139 Sets the compression dictionary to use when working with │
140 compressing or decompressing the data to be binData. Not │
141 valid for transformations that work with gzip-format │
142 data. The dictionary should consist of strings (byte │
143 sequences) that are likely to be encountered later in the │
144 data to be compressed, with the most commonly used │
145 strings preferably put towards the end of the dictionary. │
146 Tcl provides no mechanism for choosing a good such dic‐ │
147 tionary for a particular data sequence.
148
149 -header dictionary
150 Passes a description of the gzip header to create, in the
151 same format that zlib gzip understands.
152
153 -level compressionLevel
154 How hard to compress the data. Must be an integer from 0
155 (uncompressed) to 9 (maximally compressed).
156
157 -limit readaheadLimit
158 The maximum number of bytes ahead to read when decom‐
159 pressing. This defaults to 1, which ensures that data is
160 always decompressed correctly, but may be increased to
161 improve performance. This is more useful when the channel
162 is non-blocking.
163
164 Both compressing and decompressing channel transformations add
165 extra configuration options that may be accessed through chan
166 configure. The options are:
167
168 -checksum checksum
169 This read-only option gets the current checksum for the
170 uncompressed data that the compression engine has seen so
171 far. It is valid for both compressing and decompressing
172 transforms, but not for the raw inflate and deflate for‐
173 mats. The compression algorithm depends on what format is
174 being produced or consumed.
175
176 -dictionary binData
177 This read-write options gets or sets the initial compres‐ │
178 sion dictionary to use when working with compressing or │
179 decompressing the data to be binData. It is not valid │
180 for transformations that work with gzip-format data, and │
181 should not normally be set on compressing transformations │
182 other than at the point where the transformation is │
183 stacked. Note that this cannot be used to get the current │
184 active compression dictionary mid-stream, as that infor‐ │
185 mation is not exposed by the underlying library.
186
187 -flush type
188 This write-only operation flushes the current state of
189 the compressor to the underlying channel. It is only
190 valid for compressing transformations. The type must be
191 either sync or full for a normal flush or an expensive
192 flush respectively. Flushing degrades the compression
193 ratio, but makes it easier for a decompressor to recover
194 more of the file in the case of data corruption.
195
196 -header dictionary
197 This read-only option, only valid for decompressing
198 transforms that are processing gzip-format data, returns
199 the dictionary describing the header read off the data
200 stream.
201
202 -limit readaheadLimit
203 This read-write option is used by decompressing channels
204 to control the maximum number of bytes ahead to read from
205 the underlying data source. This defaults to 1, which
206 ensures that data is always decompressed correctly, but
207 may be increased to improve performance. This is more
208 useful when the channel is non-blocking.
209
210 STREAMING SUBCOMMAND
211 zlib stream mode ?options?
212 Creates a streaming compression or decompression command based
213 on the mode, and return the name of the command. For a descrip‐
214 tion of how that command works, see STREAMING INSTANCE COMMAND
215 below. The following modes and options are supported:
216
217 zlib stream compress ?-dictionary bindata? ?-level level?
218 The stream will be a compressing stream that produces
219 zlib-format output, using compression level level (if
220 specified) which will be an integer from 0 to 9, and the │
221 compression dictionary bindata (if specified).
222
223 zlib stream decompress ?-dictionary bindata?
224 The stream will be a decompressing stream that takes
225 zlib-format input and produces uncompressed output. If │
226 bindata is supplied, it is a compression dictionary to │
227 use if required.
228
229 zlib stream deflate ?-dictionary bindata? ?-level level?
230 The stream will be a compressing stream that produces raw
231 output, using compression level level (if specified)
232 which will be an integer from 0 to 9, and the compression │
233 dictionary bindata (if specified). Note that the raw com‐ │
234 pressed data includes no metadata about what compression │
235 dictionary was used, if any; that is a feature of the │
236 zlib-format data.
237
238 zlib stream gunzip
239 The stream will be a decompressing stream that takes
240 gzip-format input and produces uncompressed output.
241
242 zlib stream gzip ?-header header? ?-level level?
243 The stream will be a compressing stream that produces
244 gzip-format output, using compression level level (if
245 specified) which will be an integer from 0 to 9, and the
246 header descriptor dictionary header (if specified; for
247 keys see zlib gzip).
248
249 zlib stream inflate ?-dictionary bindata?
250 The stream will be a decompressing stream that takes raw
251 compressed input and produces uncompressed output. If │
252 bindata is supplied, it is a compression dictionary to │
253 use. Note that there are no checks in place to determine │
254 whether the compression dictionary is correct.
255
256 CHECKSUMMING SUBCOMMANDS
257 zlib adler32 string ?initValue?
258 Compute a checksum of binary string string using the Adler-32
259 algorithm. If given, initValue is used to initialize the check‐
260 sum engine.
261
262 zlib crc32 string ?initValue?
263 Compute a checksum of binary string string using the CRC-32
264 algorithm. If given, initValue is used to initialize the check‐
265 sum engine.
266
268 Streaming compression instance commands are produced by the zlib stream
269 command. They are used by calling their put subcommand one or more
270 times to load data in, and their get subcommand one or more times to
271 extract the transformed data.
272
273 The full set of subcommands supported by a streaming instance command,
274 stream, is as follows:
275
276 stream add ?option...? data
277 A short-cut for “stream put ?option...? data” followed by
278 “stream get”.
279
280 stream checksum
281 Returns the checksum of the uncompressed data seen so far by
282 this stream.
283
284 stream close
285 Deletes this stream and frees up all resources associated with
286 it.
287
288 stream eof
289 Returns a boolean indicating whether the end of the stream (as
290 determined by the compressed data itself) has been reached. Not
291 all formats support detection of the end of the stream.
292
293 stream finalize
294 A short-cut for “stream put -finalize {}”.
295
296 stream flush
297 A short-cut for “stream put -flush {}”.
298
299 stream fullflush
300 A short-cut for “stream put -fullflush {}”.
301
302 stream get ?count?
303 Return up to count bytes from stream's internal buffers with the
304 transformation applied. If count is omitted, the entire contents
305 of the buffers are returned. stream header Return the gzip
306 header description dictionary extracted from the stream. Only
307 supported for streams created with their mode parameter set to
308 gunzip.
309
310 stream put ?option...? data
311 Append the contents of the binary string data to stream's inter‐
312 nal buffers while applying the transformation. The following
313 options are supported (or an unambiguous prefix of them), which
314 are used to modify the way in which the transformation is
315 applied:
316
317 -dictionary binData
318 Sets the compression dictionary to use when working with │
319 compressing or decompressing the data to be binData.
320
321 -finalize
322 Mark the stream as finished, ensuring that all bytes have
323 been wholly compressed or decompressed. For gzip streams,
324 this also ensures that the footer is written to the
325 stream. The stream will need to be reset before having
326 more data written to it after this, though data can still
327 be read out of the stream with the get subcommand.
328
329 This option is mutually exclusive with the -flush and
330 -fullflush options.
331
332 -flush Ensure that a decompressor consuming the bytes that the
333 current (compressing) stream is producing will be able to
334 produce all the bytes that have been compressed so far,
335 at some performance penalty.
336
337 This option is mutually exclusive with the -finalize and
338 -fullflush options.
339
340 -fullflush
341 Ensure that not only can a decompressor handle all the
342 bytes produced so far (as with -flush above) but also
343 that it can restart from this point if it detects that
344 the stream is partially corrupt. This incurs a substan‐
345 tial performance penalty.
346
347 This option is mutually exclusive with the -finalize and
348 -flush options.
349
350 stream reset
351 Puts any stream, including those that have been finalized or
352 that have reached eof, back into a state where it can process
353 more data. Throws away all internally buffered data.
354
356 To compress a Tcl string, it should be first converted to a particular
357 charset encoding since the zlib command always operates on binary
358 strings.
359
360 set binData [encoding convertto utf-8 $string]
361 set compData [zlib compress $binData]
362
363 When converting back, it is also important to reverse the charset
364 encoding:
365
366 set binData [zlib decompress $compData]
367 set string [encoding convertfrom utf-8 $binData]
368
369 The compression operation from above can also be done with streams,
370 which is especially helpful when you want to accumulate the data by
371 stages:
372
373 set strm [zlib stream compress]
374 $strm put [encoding convertto utf-8 $string]
375 # ...
376 $strm finalize
377 set compData [$strm get]
378 $strm close
379
381 binary(n), chan(n), encoding(n), Tcl_ZlibDeflate(3), RFC1950 - RFC1952
382
384 compress, decompress, deflate, gzip, inflate, zlib
385
386
387
388Tcl 8.6 zlib(n)