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