Movatterモバイル変換


[0]ホーム

URL:


Navigation

13.1.zlib — Compression compatible withgzip

For applications that require data compression, the functions in this moduleallow compression and decompression, using the zlib library. The zlib libraryhas its own home page athttp://www.zlib.net. There are knownincompatibilities between the Python module and versions of the zlib libraryearlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using1.1.4 or later.

zlib’s functions have many options and often need to be used in a particularorder. This documentation doesn’t attempt to cover all of the permutations;consult the zlib manual athttp://www.zlib.net/manual.html for authoritativeinformation.

For reading and writing.gz files see thegzip module.

The available exception and functions in this module are:

exceptionzlib.error

Exception raised on compression and decompression errors.

zlib.adler32(data[,value])

Computes a Adler-32 checksum ofdata. (An Adler-32 checksum is almost asreliable as a CRC32 but can be computed much more quickly.) Ifvalue ispresent, it is used as the starting value of the checksum; otherwise, a fixeddefault value is used. This allows computing a running checksum over theconcatenation of several inputs. The algorithm is not cryptographicallystrong, and should not be used for authentication or digital signatures. Sincethe algorithm is designed for use as a checksum algorithm, it is not suitablefor use as a general hash algorithm.

Always returns an unsigned 32-bit integer.

Note

To generate the same numeric value across all Python versions andplatforms use adler32(data) & 0xffffffff. If you are only usingthe checksum in packed binary format this is not necessary as thereturn value is the correct 32bit binary representationregardless of sign.

zlib.compress(data[,level])

Compresses the bytes indata, returning a bytes object containing compressed data.level is an integer from0 to9 controlling the level of compression;1 is fastest and produces the least compression,9 is slowest andproduces the most.0 is no compression. The default value is6.Raises theerror exception if any error occurs.

zlib.compressobj(level=-1,method=DEFLATED,wbits=15,memlevel=8,strategy=Z_DEFAULT_STRATEGY[,zdict])

Returns a compression object, to be used for compressing data streams that won’tfit into memory at once.

level is the compression level – an integer from0 to9. A valueof1 is fastest and produces the least compression, while a value of9 is slowest and produces the most.0 is no compression. The defaultvalue is6.

method is the compression algorithm. Currently, the only supported value isDEFLATED.

wbits is the base two logarithm of the size of the window buffer. Thisshould be an integer from8 to15. Higher values give bettercompression, but use more memory.

memlevel controls the amount of memory used for internal compression state.Valid values range from1 to9. Higher values using more memory,but are faster and produce smaller output.

strategy is used to tune the compression algorithm. Possible values areZ_DEFAULT_STRATEGY,Z_FILTERED, andZ_HUFFMAN_ONLY.

zdict is a predefined compression dictionary. This is a sequence of bytes(such as abytes object) containing subsequences that are expectedto occur frequently in the data that is to be compressed. Those subsequencesthat are expected to be most common should come at the end of the dictionary.

Changed in version 3.3:Added thezdict parameter and keyword argument support.

zlib.crc32(data[,value])

Computes a CRC (Cyclic Redundancy Check) checksum ofdata. Ifvalue ispresent, it is used as the starting value of the checksum; otherwise, a fixeddefault value is used. This allows computing a running checksum over theconcatenation of several inputs. The algorithm is not cryptographicallystrong, and should not be used for authentication or digital signatures. Sincethe algorithm is designed for use as a checksum algorithm, it is not suitablefor use as a general hash algorithm.

Always returns an unsigned 32-bit integer.

Note

To generate the same numeric value across all Python versions andplatforms, usecrc32(data)&0xffffffff. If you are only usingthe checksum in packed binary format this is not necessary as thereturn value is the correct 32-bit binary representationregardless of sign.

zlib.decompress(data[,wbits[,bufsize]])

Decompresses the bytes indata, returning a bytes object containing theuncompressed data. Thewbits parameter controls the size of the windowbuffer, and is discussed further below.Ifbufsize is given, it is used as the initial size of the outputbuffer. Raises theerror exception if any error occurs.

The absolute value ofwbits is the base two logarithm of the size of thehistory buffer (the “window size”) used when compressing data. Its absolutevalue should be between 8 and 15 for the most recent versions of the zliblibrary, larger values resulting in better compression at the expense of greatermemory usage. When decompressing a stream,wbits must not be smallerthan the size originally used to compress the stream; using a too-smallvalue will result in an exception. The default value is therefore thehighest value, 15. Whenwbits is negative, the standardgzip header is suppressed.

bufsize is the initial size of the buffer used to hold decompressed data. Ifmore space is required, the buffer size will be increased as needed, so youdon’t have to get this value exactly right; tuning it will only save a few callstomalloc(). The default size is 16384.

zlib.decompressobj(wbits=15[,zdict])

Returns a decompression object, to be used for decompressing data streams thatwon’t fit into memory at once.

Thewbits parameter controls the size of the window buffer.

Thezdict parameter specifies a predefined compression dictionary. Ifprovided, this must be the same dictionary as was used by the compressor thatproduced the data that is to be decompressed.

Note

Ifzdict is a mutable object (such as abytearray), you must notmodify its contents between the call todecompressobj() and the firstcall to the decompressor’sdecompress() method.

Changed in version 3.3:Added thezdict parameter.

Compression objects support the following methods:

Compress.compress(data)

Compressdata, returning a bytes object containing compressed data for at leastpart of the data indata. This data should be concatenated to the outputproduced by any preceding calls to thecompress() method. Some input maybe kept in internal buffers for later processing.

Compress.flush([mode])

All pending input is processed, and a bytes object containing the remaining compressedoutput is returned.mode can be selected from the constantsZ_SYNC_FLUSH,Z_FULL_FLUSH, orZ_FINISH,defaulting toZ_FINISH.Z_SYNC_FLUSH andZ_FULL_FLUSH allow compressing further bytestrings of data, whileZ_FINISH finishes the compressed stream and prevents compressing anymore data. After callingflush() withmode set toZ_FINISH,thecompress() method cannot be called again; the only realistic action isto delete the object.

Compress.copy()

Returns a copy of the compression object. This can be used to efficientlycompress a set of data that share a common initial prefix.

Decompression objects support the following methods and attributes:

Decompress.unused_data

A bytes object which contains any bytes past the end of the compressed data. That is,this remainsb"" until the last byte that contains compression data isavailable. If the whole bytestring turned out to contain compressed data, this isb"", an empty bytes object.

Decompress.unconsumed_tail

A bytes object that contains any data that was not consumed by the lastdecompress() call because it exceeded the limit for the uncompressed databuffer. This data has not yet been seen by the zlib machinery, so you must feedit (possibly with further data concatenated to it) back to a subsequentdecompress() method call in order to get correct output.

Decompress.eof

A boolean indicating whether the end of the compressed data stream has beenreached.

This makes it possible to distinguish between a properly-formed compressedstream, and an incomplete or truncated one.

New in version 3.3.

Decompress.decompress(data[,max_length])

Decompressdata, returning a bytes object containing the uncompressed datacorresponding to at least part of the data instring. This data should beconcatenated to the output produced by any preceding calls to thedecompress() method. Some of the input data may be preserved in internalbuffers for later processing.

If the optional parametermax_length is supplied then the return value will beno longer thanmax_length. This may mean that not all of the compressed inputcan be processed; and unconsumed data will be stored in the attributeunconsumed_tail. This bytestring must be passed to a subsequent call todecompress() if decompression is to continue. Ifmax_length is notsupplied then the whole input is decompressed, andunconsumed_tail isempty.

Decompress.flush([length])

All pending input is processed, and a bytes object containing the remaininguncompressed output is returned. After callingflush(), thedecompress() method cannot be called again; the only realistic action isto delete the object.

The optional parameterlength sets the initial size of the output buffer.

Decompress.copy()

Returns a copy of the decompression object. This can be used to save the stateof the decompressor midway through the data stream in order to speed up randomseeks into the stream at a future point.

Information about the version of the zlib library in use is available throughthe following constants:

zlib.ZLIB_VERSION

The version string of the zlib library that was used for building the module.This may be different from the zlib library actually used at runtime, whichis available asZLIB_RUNTIME_VERSION.

zlib.ZLIB_RUNTIME_VERSION

The version string of the zlib library actually loaded by the interpreter.

New in version 3.3.

See also

Modulegzip
Reading and writinggzip-format files.
http://www.zlib.net
The zlib library home page.
http://www.zlib.net/manual.html
The zlib manual explains the semantics and usage of the library’s manyfunctions.

Previous topic

13. Data Compression and Archiving

Next topic

13.2.gzip — Support forgzip files

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2017, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Sep 19, 2017.Found a bug?
Created usingSphinx 1.2.

[8]ページ先頭

©2009-2025 Movatter.jp