51
51
regardless of sign.
54
.. function:: compress(string[, level])
54
.. function:: compress(data[, level])
56
Compresses the data in *string*, returning a string contained compressed data.
56
Compresses the bytes in *data*, returning a bytes object containing compressed data.
57
57
*level* is an integer from ``1`` to ``9`` controlling the level of compression;
58
58
``1`` is fastest and produces the least compression, ``9`` is slowest and
59
59
produces the most. The default value is ``6``. Raises the :exc:`error`
92
92
regardless of sign.
95
.. function:: decompress(string[, wbits[, bufsize]])
95
.. function:: decompress(data[, wbits[, bufsize]])
97
Decompresses the data in *string*, returning a string containing the
97
Decompresses the bytes in *data*, returning a bytes object containing the
98
98
uncompressed data. The *wbits* parameter controls the size of the window
99
99
buffer, and is discussed further below.
100
100
If *bufsize* is given, it is used as the initial size of the output
125
125
Compression objects support the following methods:
128
.. method:: Compress.compress(string)
128
.. method:: Compress.compress(data)
130
Compress *string*, returning a string containing compressed data for at least
131
part of the data in *string*. This data should be concatenated to the output
130
Compress *data*, returning a bytes object containing compressed data for at least
131
part of the data in *data*. This data should be concatenated to the output
132
132
produced by any preceding calls to the :meth:`compress` method. Some input may
133
133
be kept in internal buffers for later processing.
136
136
.. method:: Compress.flush([mode])
138
All pending input is processed, and a string containing the remaining compressed
138
All pending input is processed, and a bytes object containing the remaining compressed
139
139
output is returned. *mode* can be selected from the constants
140
140
:const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`,
141
141
defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and
142
:const:`Z_FULL_FLUSH` allow compressing further strings of data, while
142
:const:`Z_FULL_FLUSH` allow compressing further bytestrings of data, while
143
143
:const:`Z_FINISH` finishes the compressed stream and prevents compressing any
144
144
more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
145
145
the :meth:`compress` method cannot be called again; the only realistic action is
158
158
.. attribute:: Decompress.unused_data
160
A string which contains any bytes past the end of the compressed data. That is,
160
A bytes object which contains any bytes past the end of the compressed data. That is,
161
161
this remains ``""`` until the last byte that contains compression data is
162
available. If the whole string turned out to contain compressed data, this is
163
``""``, the empty string.
162
available. If the whole bytestring turned out to contain compressed data, this is
163
``b""``, an empty bytes object.
165
The only way to determine where a string of compressed data ends is by actually
165
The only way to determine where a bytestring of compressed data ends is by actually
166
166
decompressing it. This means that when compressed data is contained part of a
167
167
larger file, you can only find the end of it by reading data and feeding it
168
followed by some non-empty string into a decompression object's
168
followed by some non-empty bytestring into a decompression object's
169
169
:meth:`decompress` method until the :attr:`unused_data` attribute is no longer
173
173
.. attribute:: Decompress.unconsumed_tail
175
A string that contains any data that was not consumed by the last
175
A bytes object that contains any data that was not consumed by the last
176
176
:meth:`decompress` call because it exceeded the limit for the uncompressed data
177
177
buffer. This data has not yet been seen by the zlib machinery, so you must feed
178
178
it (possibly with further data concatenated to it) back to a subsequent
179
179
:meth:`decompress` method call in order to get correct output.
182
.. method:: Decompress.decompress(string[, max_length])
182
.. method:: Decompress.decompress(data[, max_length])
184
Decompress *string*, returning a string containing the uncompressed data
184
Decompress *data*, returning a bytes object containing the uncompressed data
185
185
corresponding to at least part of the data in *string*. This data should be
186
186
concatenated to the output produced by any preceding calls to the
187
187
:meth:`decompress` method. Some of the input data may be preserved in internal
190
190
If the optional parameter *max_length* is supplied then the return value will be
191
191
no longer than *max_length*. This may mean that not all of the compressed input
192
192
can be processed; and unconsumed data will be stored in the attribute
193
:attr:`unconsumed_tail`. This string must be passed to a subsequent call to
193
:attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to
194
194
:meth:`decompress` if decompression is to continue. If *max_length* is not
195
supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is an
195
supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is
199
199
.. method:: Decompress.flush([length])
201
All pending input is processed, and a string containing the remaining
201
All pending input is processed, and a bytes object containing the remaining
202
202
uncompressed output is returned. After calling :meth:`flush`, the
203
203
:meth:`decompress` method cannot be called again; the only realistic action is
204
204
to delete the object.