blob: 3f051a70b2c03b0f16994715e750f3673410c3a0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`zlib` --- Compression compatible with :program:`gzip`
3===========================================================
4
5.. module:: zlib
6 :synopsis: Low-level interface to compression and decompression routines compatible with
7 gzip.
8
9
10For applications that require data compression, the functions in this module
11allow compression and decompression, using the zlib library. The zlib library
12has its own home page at http://www.zlib.net. There are known
13incompatibilities between the Python module and versions of the zlib library
14earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using
151.1.4 or later.
16
17zlib's functions have many options and often need to be used in a particular
18order. This documentation doesn't attempt to cover all of the permutations;
19consult the zlib manual at http://www.zlib.net/manual.html for authoritative
20information.
21
22The available exception and functions in this module are:
23
24
25.. exception:: error
26
27 Exception raised on compression and decompression errors.
28
29
30.. function:: adler32(string[, value])
31
32 Computes a Adler-32 checksum of *string*. (An Adler-32 checksum is almost as
33 reliable as a CRC32 but can be computed much more quickly.) If *value* is
34 present, it is used as the starting value of the checksum; otherwise, a fixed
35 default value is used. This allows computing a running checksum over the
36 concatenation of several input strings. The algorithm is not cryptographically
37 strong, and should not be used for authentication or digital signatures. Since
38 the algorithm is designed for use as a checksum algorithm, it is not suitable
39 for use as a general hash algorithm.
40
41
42.. function:: compress(string[, level])
43
44 Compresses the data in *string*, returning a string contained compressed data.
45 *level* is an integer from ``1`` to ``9`` controlling the level of compression;
46 ``1`` is fastest and produces the least compression, ``9`` is slowest and
47 produces the most. The default value is ``6``. Raises the :exc:`error`
48 exception if any error occurs.
49
50
51.. function:: compressobj([level])
52
53 Returns a compression object, to be used for compressing data streams that won't
54 fit into memory at once. *level* is an integer from ``1`` to ``9`` controlling
55 the level of compression; ``1`` is fastest and produces the least compression,
56 ``9`` is slowest and produces the most. The default value is ``6``.
57
58
59.. function:: crc32(string[, value])
60
61 .. index::
62 single: Cyclic Redundancy Check
63 single: checksum; Cyclic Redundancy Check
64
65 Computes a CRC (Cyclic Redundancy Check) checksum of *string*. If *value* is
66 present, it is used as the starting value of the checksum; otherwise, a fixed
67 default value is used. This allows computing a running checksum over the
68 concatenation of several input strings. The algorithm is not cryptographically
69 strong, and should not be used for authentication or digital signatures. Since
70 the algorithm is designed for use as a checksum algorithm, it is not suitable
71 for use as a general hash algorithm.
72
73 .. %
74
75
76.. function:: decompress(string[, wbits[, bufsize]])
77
78 Decompresses the data in *string*, returning a string containing the
79 uncompressed data. The *wbits* parameter controls the size of the window
80 buffer. If *bufsize* is given, it is used as the initial size of the output
81 buffer. Raises the :exc:`error` exception if any error occurs.
82
83 The absolute value of *wbits* is the base two logarithm of the size of the
84 history buffer (the "window size") used when compressing data. Its absolute
85 value should be between 8 and 15 for the most recent versions of the zlib
86 library, larger values resulting in better compression at the expense of greater
87 memory usage. The default value is 15. When *wbits* is negative, the standard
88 :program:`gzip` header is suppressed; this is an undocumented feature of the
89 zlib library, used for compatibility with :program:`unzip`'s compression file
90 format.
91
92 *bufsize* is the initial size of the buffer used to hold decompressed data. If
93 more space is required, the buffer size will be increased as needed, so you
94 don't have to get this value exactly right; tuning it will only save a few calls
95 to :cfunc:`malloc`. The default size is 16384.
96
97
98.. function:: decompressobj([wbits])
99
100 Returns a decompression object, to be used for decompressing data streams that
101 won't fit into memory at once. The *wbits* parameter controls the size of the
102 window buffer.
103
104Compression objects support the following methods:
105
106
107.. method:: Compress.compress(string)
108
109 Compress *string*, returning a string containing compressed data for at least
110 part of the data in *string*. This data should be concatenated to the output
111 produced by any preceding calls to the :meth:`compress` method. Some input may
112 be kept in internal buffers for later processing.
113
114
115.. method:: Compress.flush([mode])
116
117 All pending input is processed, and a string containing the remaining compressed
118 output is returned. *mode* can be selected from the constants
119 :const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`,
120 defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and
121 :const:`Z_FULL_FLUSH` allow compressing further strings of data, while
122 :const:`Z_FINISH` finishes the compressed stream and prevents compressing any
123 more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
124 the :meth:`compress` method cannot be called again; the only realistic action is
125 to delete the object.
126
127
128.. method:: Compress.copy()
129
130 Returns a copy of the compression object. This can be used to efficiently
131 compress a set of data that share a common initial prefix.
132
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134Decompression objects support the following methods, and two attributes:
135
136
137.. attribute:: Decompress.unused_data
138
139 A string which contains any bytes past the end of the compressed data. That is,
140 this remains ``""`` until the last byte that contains compression data is
141 available. If the whole string turned out to contain compressed data, this is
142 ``""``, the empty string.
143
144 The only way to determine where a string of compressed data ends is by actually
145 decompressing it. This means that when compressed data is contained part of a
146 larger file, you can only find the end of it by reading data and feeding it
147 followed by some non-empty string into a decompression object's
148 :meth:`decompress` method until the :attr:`unused_data` attribute is no longer
149 the empty string.
150
151
152.. attribute:: Decompress.unconsumed_tail
153
154 A string that contains any data that was not consumed by the last
155 :meth:`decompress` call because it exceeded the limit for the uncompressed data
156 buffer. This data has not yet been seen by the zlib machinery, so you must feed
157 it (possibly with further data concatenated to it) back to a subsequent
158 :meth:`decompress` method call in order to get correct output.
159
160
161.. method:: Decompress.decompress(string[, max_length])
162
163 Decompress *string*, returning a string containing the uncompressed data
164 corresponding to at least part of the data in *string*. This data should be
165 concatenated to the output produced by any preceding calls to the
166 :meth:`decompress` method. Some of the input data may be preserved in internal
167 buffers for later processing.
168
169 If the optional parameter *max_length* is supplied then the return value will be
170 no longer than *max_length*. This may mean that not all of the compressed input
171 can be processed; and unconsumed data will be stored in the attribute
172 :attr:`unconsumed_tail`. This string must be passed to a subsequent call to
173 :meth:`decompress` if decompression is to continue. If *max_length* is not
174 supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is an
175 empty string.
176
177
178.. method:: Decompress.flush([length])
179
180 All pending input is processed, and a string containing the remaining
181 uncompressed output is returned. After calling :meth:`flush`, the
182 :meth:`decompress` method cannot be called again; the only realistic action is
183 to delete the object.
184
185 The optional parameter *length* sets the initial size of the output buffer.
186
187
188.. method:: Decompress.copy()
189
190 Returns a copy of the decompression object. This can be used to save the state
191 of the decompressor midway through the data stream in order to speed up random
192 seeks into the stream at a future point.
193
Georg Brandl116aa622007-08-15 14:28:22 +0000194
195.. seealso::
196
197 Module :mod:`gzip`
198 Reading and writing :program:`gzip`\ -format files.
199
200 http://www.zlib.net
201 The zlib library home page.
202
203 http://www.zlib.net/manual.html
204 The zlib manual explains the semantics and usage of the library's many
205 functions.
206