Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 1 | % XXX The module has been extended (by Jeremy) but this documentation |
| 2 | % hasn't been updated yet |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 3 | |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 4 | \section{Built-in Module \module{zlib}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 5 | \label{module-zlib} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 6 | \bimodindex{zlib} |
| 7 | |
| 8 | For applications that require data compression, the functions in this |
Guido van Rossum | 412154f | 1997-04-30 19:39:21 +0000 | [diff] [blame] | 9 | module allow compression and decompression, using the zlib library, |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 10 | which is based on the GNU \program{gzip} program. The zlib library |
| 11 | has its own home page at |
| 12 | \url{http://www.cdrom.com/pub/infozip/zlib/}. Version 1.0.4 is the |
| 13 | most recent version as of December, 1997; use a later version if one |
| 14 | is available. |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 15 | |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 16 | The available exception and functions in this module are: |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 17 | |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 18 | \begin{excdesc}{error} |
| 19 | Exception raised on compression and decompression errors. |
| 20 | \end{excdesc} |
| 21 | |
| 22 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 23 | \begin{funcdesc}{adler32}{string\optional{, value}} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 24 | Computes a Adler-32 checksum of \var{string}. (An Adler-32 |
| 25 | checksum is almost as reliable as a CRC32 but can be computed much |
| 26 | more quickly.) If \var{value} is present, it is used as the |
| 27 | starting value of the checksum; otherwise, a fixed default value is |
| 28 | used. This allows computing a running checksum over the |
| 29 | concatenation of several input strings. The algorithm is not |
| 30 | cryptographically strong, and should not be used for |
| 31 | authentication or digital signatures. |
| 32 | \end{funcdesc} |
| 33 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 34 | \begin{funcdesc}{compress}{string\optional{, level}} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 35 | Compresses the data in \var{string}, returning a string contained |
Fred Drake | ed79783 | 1998-01-22 16:11:18 +0000 | [diff] [blame] | 36 | compressed data. \var{level} is an integer from \code{1} to \code{9} |
| 37 | controlling the level of compression; \code{1} is fastest and produces |
| 38 | the least compression, \code{9} is slowest and produces the most. The |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 39 | default value is \code{6}. Raises the \exception{error} |
Fred Drake | ed79783 | 1998-01-22 16:11:18 +0000 | [diff] [blame] | 40 | exception if any error occurs. |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 41 | \end{funcdesc} |
| 42 | |
| 43 | \begin{funcdesc}{compressobj}{\optional{level}} |
Fred Drake | ed79783 | 1998-01-22 16:11:18 +0000 | [diff] [blame] | 44 | Returns a compression object, to be used for compressing data streams |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 45 | that won't fit into memory at once. \var{level} is an integer from |
Fred Drake | ed79783 | 1998-01-22 16:11:18 +0000 | [diff] [blame] | 46 | \code{1} to \code{9} controlling the level of compression; \code{1} is |
| 47 | fastest and produces the least compression, \code{9} is slowest and |
| 48 | produces the most. The default value is \code{6}. |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 49 | \end{funcdesc} |
| 50 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 51 | \begin{funcdesc}{crc32}{string\optional{, value}} |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 52 | Computes a CRC (Cyclic Redundancy Check)% |
| 53 | \index{Cyclic Redundancy Check} |
| 54 | checksum of \var{string}. If |
| 55 | \var{value} is present, it is used as the starting value of the |
| 56 | checksum; otherwise, a fixed default value is used. This allows |
| 57 | computing a running checksum over the concatenation of several |
| 58 | input strings. The algorithm is not cryptographically strong, and |
| 59 | should not be used for authentication or digital signatures. |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 60 | \end{funcdesc} |
| 61 | |
| 62 | \begin{funcdesc}{decompress}{string} |
| 63 | Decompresses the data in \var{string}, returning a string containing |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 64 | the uncompressed data. Raises the \exception{error} exception if any |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 65 | error occurs. |
| 66 | \end{funcdesc} |
| 67 | |
| 68 | \begin{funcdesc}{decompressobj}{\optional{wbits}} |
| 69 | Returns a compression object, to be used for decompressing data streams |
Guido van Rossum | 412154f | 1997-04-30 19:39:21 +0000 | [diff] [blame] | 70 | that won't fit into memory at once. The \var{wbits} parameter |
| 71 | controls the size of the window buffer; usually this can be left |
| 72 | alone. |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 73 | \end{funcdesc} |
| 74 | |
| 75 | Compression objects support the following methods: |
| 76 | |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 77 | \begin{methoddesc}[Compress]{compress}{string} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 78 | Compress \var{string}, returning a string containing compressed data |
| 79 | for at least part of the data in \var{string}. This data should be |
| 80 | concatenated to the output produced by any preceding calls to the |
Fred Drake | ed79783 | 1998-01-22 16:11:18 +0000 | [diff] [blame] | 81 | \method{compress()} method. Some input may be kept in internal buffers |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 82 | for later processing. |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 83 | \end{methoddesc} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 84 | |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 85 | \begin{methoddesc}[Compress]{flush}{} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 86 | All pending input is processed, and an string containing the remaining |
Fred Drake | ed79783 | 1998-01-22 16:11:18 +0000 | [diff] [blame] | 87 | compressed output is returned. After calling \method{flush()}, the |
| 88 | \method{compress()} method cannot be called again; the only realistic |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 89 | action is to delete the object. |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 90 | \end{methoddesc} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 91 | |
| 92 | Decompression objects support the following methods: |
| 93 | |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 94 | \begin{methoddesc}[Decompress]{decompress}{string} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 95 | Decompress \var{string}, returning a string containing the |
| 96 | uncompressed data corresponding to at least part of the data in |
| 97 | \var{string}. This data should be concatenated to the output produced |
| 98 | by any preceding calls to the |
Fred Drake | ed79783 | 1998-01-22 16:11:18 +0000 | [diff] [blame] | 99 | \method{decompress()} method. Some of the input data may be preserved |
Guido van Rossum | 412154f | 1997-04-30 19:39:21 +0000 | [diff] [blame] | 100 | in internal buffers for later processing. |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 101 | \end{methoddesc} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 102 | |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 103 | \begin{methoddesc}[Decompress]{flush}{} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 104 | All pending input is processed, and a string containing the remaining |
Fred Drake | ed79783 | 1998-01-22 16:11:18 +0000 | [diff] [blame] | 105 | uncompressed output is returned. After calling \method{flush()}, the |
| 106 | \method{decompress()} method cannot be called again; the only realistic |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 107 | action is to delete the object. |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 108 | \end{methoddesc} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 110 | \begin{seealso} |
Fred Drake | 74810d5 | 1998-04-03 06:49:26 +0000 | [diff] [blame^] | 111 | \seemodule{gzip}{reading and writing \program{gzip}-format files} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 112 | \end{seealso} |
Guido van Rossum | 04bc9d6 | 1997-04-30 18:12:27 +0000 | [diff] [blame] | 113 | |
| 114 | |