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