Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`zlib` --- Compression compatible with :program:`gzip` |
| 2 | =========================================================== |
| 3 | |
| 4 | .. module:: zlib |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 5 | :synopsis: Low-level interface to compression and decompression routines |
| 6 | compatible with gzip. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 8 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
| 10 | For applications that require data compression, the functions in this module |
| 11 | allow compression and decompression, using the zlib library. The zlib library |
| 12 | has its own home page at http://www.zlib.net. There are known |
| 13 | incompatibilities between the Python module and versions of the zlib library |
| 14 | earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using |
| 15 | 1.1.4 or later. |
| 16 | |
| 17 | zlib's functions have many options and often need to be used in a particular |
| 18 | order. This documentation doesn't attempt to cover all of the permutations; |
| 19 | consult the zlib manual at http://www.zlib.net/manual.html for authoritative |
| 20 | information. |
| 21 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 22 | For reading and writing ``.gz`` files see the :mod:`gzip` module. |
Guido van Rossum | 7767711 | 2007-11-05 19:43:04 +0000 | [diff] [blame] | 23 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | The available exception and functions in this module are: |
| 25 | |
| 26 | |
| 27 | .. exception:: error |
| 28 | |
| 29 | Exception raised on compression and decompression errors. |
| 30 | |
| 31 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 32 | .. function:: adler32(data[, value]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 34 | Computes an Adler-32 checksum of *data*. (An Adler-32 checksum is almost as |
Martin Panter | b82032f | 2015-12-11 05:19:29 +0000 | [diff] [blame] | 35 | reliable as a CRC32 but can be computed much more quickly.) The result |
| 36 | is an unsigned 32-bit integer. If *value* is present, it is used as |
| 37 | the starting value of the checksum; otherwise, a default value of 1 |
| 38 | is used. Passing in *value* allows computing a running checksum over the |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 39 | concatenation of several inputs. The algorithm is not cryptographically |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | strong, and should not be used for authentication or digital signatures. Since |
| 41 | the algorithm is designed for use as a checksum algorithm, it is not suitable |
| 42 | for use as a general hash algorithm. |
| 43 | |
Martin Panter | b82032f | 2015-12-11 05:19:29 +0000 | [diff] [blame] | 44 | .. versionchanged:: 3.0 |
| 45 | Always returns an unsigned value. |
| 46 | To generate the same numeric value across all Python versions and |
| 47 | platforms, use ``adler32(data) & 0xffffffff``. |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 48 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 50 | .. function:: compress(data, level=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 52 | Compresses the bytes in *data*, returning a bytes object containing compressed data. |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 53 | *level* is an integer from ``0`` to ``9`` or ``-1`` controlling the level of compression; |
Xiang Zhang | bc3f228 | 2018-03-07 13:05:37 +0800 | [diff] [blame] | 54 | ``1`` (Z_BEST_SPEED) is fastest and produces the least compression, ``9`` (Z_BEST_COMPRESSION) |
| 55 | is slowest and produces the most. ``0`` (Z_NO_COMPRESSION) is no compression. |
| 56 | The default value is ``-1`` (Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 57 | compromise between speed and compression (currently equivalent to level 6). |
Nadeem Vawda | 6ff262e | 2012-11-11 14:14:47 +0100 | [diff] [blame] | 58 | Raises the :exc:`error` exception if any error occurs. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 60 | .. versionchanged:: 3.6 |
Serhiy Storchaka | 2d8f945 | 2016-06-25 22:47:04 +0300 | [diff] [blame] | 61 | *level* can now be used as a keyword parameter. |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 62 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
Xiang Zhang | bc3f228 | 2018-03-07 13:05:37 +0800 | [diff] [blame] | 64 | .. function:: compressobj(level=-1, method=DEFLATED, wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=Z_DEFAULT_STRATEGY[, zdict]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
| 66 | Returns a compression object, to be used for compressing data streams that won't |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 67 | fit into memory at once. |
| 68 | |
Martin Panter | 567d513 | 2016-02-03 07:06:33 +0000 | [diff] [blame] | 69 | *level* is the compression level -- an integer from ``0`` to ``9`` or ``-1``. |
Xiang Zhang | bc3f228 | 2018-03-07 13:05:37 +0800 | [diff] [blame] | 70 | A value of ``1`` (Z_BEST_SPEED) is fastest and produces the least compression, |
| 71 | while a value of ``9`` (Z_BEST_COMPRESSION) is slowest and produces the most. |
| 72 | ``0`` (Z_NO_COMPRESSION) is no compression. The default value is ``-1`` (Z_DEFAULT_COMPRESSION). |
| 73 | Z_DEFAULT_COMPRESSION represents a default compromise between speed and compression |
| 74 | (currently equivalent to level 6). |
Nadeem Vawda | 2180c97 | 2012-06-22 01:40:49 +0200 | [diff] [blame] | 75 | |
| 76 | *method* is the compression algorithm. Currently, the only supported value is |
Xiang Zhang | bc3f228 | 2018-03-07 13:05:37 +0800 | [diff] [blame] | 77 | :const:`DEFLATED`. |
Nadeem Vawda | 2180c97 | 2012-06-22 01:40:49 +0200 | [diff] [blame] | 78 | |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 79 | The *wbits* argument controls the size of the history buffer (or the |
| 80 | "window size") used when compressing data, and whether a header and |
Xiang Zhang | bc3f228 | 2018-03-07 13:05:37 +0800 | [diff] [blame] | 81 | trailer is included in the output. It can take several ranges of values, |
| 82 | defaulting to ``15`` (MAX_WBITS): |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 83 | |
| 84 | * +9 to +15: The base-two logarithm of the window size, which |
| 85 | therefore ranges between 512 and 32768. Larger values produce |
| 86 | better compression at the expense of greater memory usage. The |
| 87 | resulting output will include a zlib-specific header and trailer. |
| 88 | |
| 89 | * −9 to −15: Uses the absolute value of *wbits* as the |
| 90 | window size logarithm, while producing a raw output stream with no |
| 91 | header or trailing checksum. |
| 92 | |
| 93 | * +25 to +31 = 16 + (9 to 15): Uses the low 4 bits of the value as the |
| 94 | window size logarithm, while including a basic :program:`gzip` header |
| 95 | and trailing checksum in the output. |
Nadeem Vawda | 2180c97 | 2012-06-22 01:40:49 +0200 | [diff] [blame] | 96 | |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 97 | The *memLevel* argument controls the amount of memory used for the |
| 98 | internal compression state. Valid values range from ``1`` to ``9``. |
| 99 | Higher values use more memory, but are faster and produce smaller output. |
Nadeem Vawda | 2180c97 | 2012-06-22 01:40:49 +0200 | [diff] [blame] | 100 | |
| 101 | *strategy* is used to tune the compression algorithm. Possible values are |
Xiang Zhang | bc3f228 | 2018-03-07 13:05:37 +0800 | [diff] [blame] | 102 | :const:`Z_DEFAULT_STRATEGY`, :const:`Z_FILTERED`, :const:`Z_HUFFMAN_ONLY`, |
| 103 | :const:`Z_RLE` (zlib 1.2.0.1) and :const:`Z_FIXED` (zlib 1.2.2.2). |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 104 | |
| 105 | *zdict* is a predefined compression dictionary. This is a sequence of bytes |
| 106 | (such as a :class:`bytes` object) containing subsequences that are expected |
| 107 | to occur frequently in the data that is to be compressed. Those subsequences |
| 108 | that are expected to be most common should come at the end of the dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
Georg Brandl | 9aae9e5 | 2012-06-26 08:51:17 +0200 | [diff] [blame] | 110 | .. versionchanged:: 3.3 |
Georg Brandl | 9ff06dc | 2013-10-17 19:51:34 +0200 | [diff] [blame] | 111 | Added the *zdict* parameter and keyword argument support. |
Georg Brandl | 9aae9e5 | 2012-06-26 08:51:17 +0200 | [diff] [blame] | 112 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 114 | .. function:: crc32(data[, value]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | |
| 116 | .. index:: |
| 117 | single: Cyclic Redundancy Check |
| 118 | single: checksum; Cyclic Redundancy Check |
| 119 | |
Martin Panter | b82032f | 2015-12-11 05:19:29 +0000 | [diff] [blame] | 120 | Computes a CRC (Cyclic Redundancy Check) checksum of *data*. The |
| 121 | result is an unsigned 32-bit integer. If *value* is present, it is used |
| 122 | as the starting value of the checksum; otherwise, a default value of 0 |
| 123 | is used. Passing in *value* allows computing a running checksum over the |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 124 | concatenation of several inputs. The algorithm is not cryptographically |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | strong, and should not be used for authentication or digital signatures. Since |
| 126 | the algorithm is designed for use as a checksum algorithm, it is not suitable |
| 127 | for use as a general hash algorithm. |
| 128 | |
Martin Panter | b82032f | 2015-12-11 05:19:29 +0000 | [diff] [blame] | 129 | .. versionchanged:: 3.0 |
| 130 | Always returns an unsigned value. |
Georg Brandl | 9aae9e5 | 2012-06-26 08:51:17 +0200 | [diff] [blame] | 131 | To generate the same numeric value across all Python versions and |
Martin Panter | b82032f | 2015-12-11 05:19:29 +0000 | [diff] [blame] | 132 | platforms, use ``crc32(data) & 0xffffffff``. |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 133 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 135 | .. function:: decompress(data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 137 | Decompresses the bytes in *data*, returning a bytes object containing the |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 138 | uncompressed data. The *wbits* parameter depends on |
| 139 | the format of *data*, and is discussed further below. |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 140 | If *bufsize* is given, it is used as the initial size of the output |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | buffer. Raises the :exc:`error` exception if any error occurs. |
| 142 | |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 143 | .. _decompress-wbits: |
| 144 | |
| 145 | The *wbits* parameter controls the size of the history buffer |
| 146 | (or "window size"), and what header and trailer format is expected. |
| 147 | It is similar to the parameter for :func:`compressobj`, but accepts |
| 148 | more ranges of values: |
| 149 | |
| 150 | * +8 to +15: The base-two logarithm of the window size. The input |
| 151 | must include a zlib header and trailer. |
| 152 | |
| 153 | * 0: Automatically determine the window size from the zlib header. |
Martin Panter | c618ae8 | 2016-05-27 11:20:21 +0000 | [diff] [blame] | 154 | Only supported since zlib 1.2.3.5. |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 155 | |
| 156 | * −8 to −15: Uses the absolute value of *wbits* as the window size |
| 157 | logarithm. The input must be a raw stream with no header or trailer. |
| 158 | |
| 159 | * +24 to +31 = 16 + (8 to 15): Uses the low 4 bits of the value as |
| 160 | the window size logarithm. The input must include a gzip header and |
| 161 | trailer. |
| 162 | |
| 163 | * +40 to +47 = 32 + (8 to 15): Uses the low 4 bits of the value as |
| 164 | the window size logarithm, and automatically accepts either |
| 165 | the zlib or gzip format. |
| 166 | |
| 167 | When decompressing a stream, the window size must not be smaller |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 168 | than the size originally used to compress the stream; using a too-small |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 169 | value may result in an :exc:`error` exception. The default *wbits* value |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 170 | corresponds to the largest window size and requires a zlib header and |
| 171 | trailer to be included. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | |
| 173 | *bufsize* is the initial size of the buffer used to hold decompressed data. If |
| 174 | more space is required, the buffer size will be increased as needed, so you |
| 175 | don't have to get this value exactly right; tuning it will only save a few calls |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 176 | to :c:func:`malloc`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 178 | .. versionchanged:: 3.6 |
| 179 | *wbits* and *bufsize* can be used as keyword arguments. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
Xiang Zhang | bc3f228 | 2018-03-07 13:05:37 +0800 | [diff] [blame] | 181 | .. function:: decompressobj(wbits=MAX_WBITS[, zdict]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
| 183 | Returns a decompression object, to be used for decompressing data streams that |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 184 | won't fit into memory at once. |
| 185 | |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 186 | The *wbits* parameter controls the size of the history buffer (or the |
| 187 | "window size"), and what header and trailer format is expected. It has |
| 188 | the same meaning as `described for decompress() <#decompress-wbits>`__. |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 189 | |
| 190 | The *zdict* parameter specifies a predefined compression dictionary. If |
| 191 | provided, this must be the same dictionary as was used by the compressor that |
| 192 | produced the data that is to be decompressed. |
| 193 | |
Georg Brandl | 9aae9e5 | 2012-06-26 08:51:17 +0200 | [diff] [blame] | 194 | .. note:: |
| 195 | |
| 196 | If *zdict* is a mutable object (such as a :class:`bytearray`), you must not |
| 197 | modify its contents between the call to :func:`decompressobj` and the first |
| 198 | call to the decompressor's ``decompress()`` method. |
| 199 | |
| 200 | .. versionchanged:: 3.3 |
| 201 | Added the *zdict* parameter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
Nadeem Vawda | 64d25dd | 2011-09-12 00:04:13 +0200 | [diff] [blame] | 203 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 | Compression objects support the following methods: |
| 205 | |
| 206 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 207 | .. method:: Compress.compress(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 209 | Compress *data*, returning a bytes object containing compressed data for at least |
| 210 | part of the data in *data*. This data should be concatenated to the output |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | produced by any preceding calls to the :meth:`compress` method. Some input may |
| 212 | be kept in internal buffers for later processing. |
| 213 | |
| 214 | |
| 215 | .. method:: Compress.flush([mode]) |
| 216 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 217 | All pending input is processed, and a bytes object containing the remaining compressed |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | output is returned. *mode* can be selected from the constants |
Xiang Zhang | bc3f228 | 2018-03-07 13:05:37 +0800 | [diff] [blame] | 219 | :const:`Z_NO_FLUSH`, :const:`Z_PARTIAL_FLUSH`, :const:`Z_SYNC_FLUSH`, |
| 220 | :const:`Z_FULL_FLUSH`, :const:`Z_BLOCK` (zlib 1.2.3.4), or :const:`Z_FINISH`, |
| 221 | defaulting to :const:`Z_FINISH`. Except :const:`Z_FINISH`, all constants |
| 222 | allow compressing further bytestrings of data, while :const:`Z_FINISH` finishes the |
| 223 | compressed stream and prevents compressing any more data. After calling :meth:`flush` |
| 224 | with *mode* set to :const:`Z_FINISH`, the :meth:`compress` method cannot be called again; |
| 225 | the only realistic action is to delete the object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 226 | |
| 227 | |
| 228 | .. method:: Compress.copy() |
| 229 | |
| 230 | Returns a copy of the compression object. This can be used to efficiently |
| 231 | compress a set of data that share a common initial prefix. |
| 232 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame^] | 234 | .. versionchanged:: 3.8 |
| 235 | Added :func:`copy.copy` and :func:`copy.deepcopy` support to compression |
| 236 | objects. |
| 237 | |
| 238 | |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 239 | Decompression objects support the following methods and attributes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
| 241 | |
| 242 | .. attribute:: Decompress.unused_data |
| 243 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 244 | A bytes object which contains any bytes past the end of the compressed data. That is, |
Serhiy Storchaka | 5e028ae | 2014-02-06 21:10:41 +0200 | [diff] [blame] | 245 | this remains ``b""`` until the last byte that contains compression data is |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 246 | available. If the whole bytestring turned out to contain compressed data, this is |
| 247 | ``b""``, an empty bytes object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
| 250 | .. attribute:: Decompress.unconsumed_tail |
| 251 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 252 | A bytes object that contains any data that was not consumed by the last |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 253 | :meth:`decompress` call because it exceeded the limit for the uncompressed data |
| 254 | buffer. This data has not yet been seen by the zlib machinery, so you must feed |
| 255 | it (possibly with further data concatenated to it) back to a subsequent |
| 256 | :meth:`decompress` method call in order to get correct output. |
| 257 | |
| 258 | |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 259 | .. attribute:: Decompress.eof |
| 260 | |
| 261 | A boolean indicating whether the end of the compressed data stream has been |
| 262 | reached. |
| 263 | |
| 264 | This makes it possible to distinguish between a properly-formed compressed |
| 265 | stream, and an incomplete or truncated one. |
| 266 | |
| 267 | .. versionadded:: 3.3 |
| 268 | |
| 269 | |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 270 | .. method:: Decompress.decompress(data, max_length=0) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 271 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 272 | Decompress *data*, returning a bytes object containing the uncompressed data |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | corresponding to at least part of the data in *string*. This data should be |
| 274 | concatenated to the output produced by any preceding calls to the |
| 275 | :meth:`decompress` method. Some of the input data may be preserved in internal |
| 276 | buffers for later processing. |
| 277 | |
Martin Panter | 38fe4dc | 2015-11-18 00:59:17 +0000 | [diff] [blame] | 278 | If the optional parameter *max_length* is non-zero then the return value will be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 279 | no longer than *max_length*. This may mean that not all of the compressed input |
| 280 | can be processed; and unconsumed data will be stored in the attribute |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 281 | :attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 282 | :meth:`decompress` if decompression is to continue. If *max_length* is zero |
| 283 | then the whole input is decompressed, and :attr:`unconsumed_tail` is empty. |
| 284 | |
| 285 | .. versionchanged:: 3.6 |
| 286 | *max_length* can be used as a keyword argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 287 | |
| 288 | |
| 289 | .. method:: Decompress.flush([length]) |
| 290 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 291 | All pending input is processed, and a bytes object containing the remaining |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | uncompressed output is returned. After calling :meth:`flush`, the |
| 293 | :meth:`decompress` method cannot be called again; the only realistic action is |
| 294 | to delete the object. |
| 295 | |
| 296 | The optional parameter *length* sets the initial size of the output buffer. |
| 297 | |
| 298 | |
| 299 | .. method:: Decompress.copy() |
| 300 | |
| 301 | Returns a copy of the decompression object. This can be used to save the state |
| 302 | of the decompressor midway through the data stream in order to speed up random |
| 303 | seeks into the stream at a future point. |
| 304 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 305 | |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame^] | 306 | .. versionchanged:: 3.8 |
| 307 | Added :func:`copy.copy` and :func:`copy.deepcopy` support to decompression |
| 308 | objects. |
| 309 | |
| 310 | |
Nadeem Vawda | 64d25dd | 2011-09-12 00:04:13 +0200 | [diff] [blame] | 311 | Information about the version of the zlib library in use is available through |
| 312 | the following constants: |
| 313 | |
| 314 | |
| 315 | .. data:: ZLIB_VERSION |
| 316 | |
| 317 | The version string of the zlib library that was used for building the module. |
| 318 | This may be different from the zlib library actually used at runtime, which |
| 319 | is available as :const:`ZLIB_RUNTIME_VERSION`. |
| 320 | |
Nadeem Vawda | 64d25dd | 2011-09-12 00:04:13 +0200 | [diff] [blame] | 321 | |
| 322 | .. data:: ZLIB_RUNTIME_VERSION |
| 323 | |
| 324 | The version string of the zlib library actually loaded by the interpreter. |
| 325 | |
| 326 | .. versionadded:: 3.3 |
| 327 | |
| 328 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 329 | .. seealso:: |
| 330 | |
| 331 | Module :mod:`gzip` |
| 332 | Reading and writing :program:`gzip`\ -format files. |
| 333 | |
| 334 | http://www.zlib.net |
| 335 | The zlib library home page. |
| 336 | |
| 337 | http://www.zlib.net/manual.html |
| 338 | The zlib manual explains the semantics and usage of the library's many |
| 339 | functions. |
| 340 | |