zlibCompression compatible with gzip压缩兼容gzip


For applications that require data compression, the functions in this module allow compression and decompression, using the zlib library. 对于需要数据压缩的应用程序,此模块中的函数允许使用zlib库进行压缩和解压缩。The zlib library has its own home page at https://www.zlib.net. zlib库有自己的主页https://www.zlib.netThere are known incompatibilities between the Python module and versions of the zlib library earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using 1.1.4 or later.Python模块与zlib库1.1.3之前的版本之间存在已知的不兼容性;1.1.3有一个安全漏洞,因此我们建议使用1.1.4或更晚版本。

zlib’s functions have many options and often need to be used in a particular order. zlib的函数有很多选项,通常需要按特定顺序使用。This documentation doesn’t attempt to cover all of the permutations; consult the zlib manual at http://www.zlib.net/manual.html for authoritative information.本文档并不试图涵盖所有排列;请参阅zlib手册http://www.zlib.net/manual.html获取权威信息。

For reading and writing .gz files see the gzip module.有关读取和写入.gz文件的信息,请参阅gzip模块。

The available exception and functions in this module are:此模块中可用的异常和功能包括:

exceptionzlib.error

Exception raised on compression and decompression errors.压缩和解压缩错误引发异常。

zlib.adler32(data[, value])

Computes an Adler-32 checksum of data. 计算数据的Adler-32校验和。(An Adler-32 checksum is almost as reliable as a CRC32 but can be computed much more quickly.) (Adler-32校验和几乎与CRC32一样可靠,但计算速度更快。)The result is an unsigned 32-bit integer. 结果是一个无符号32位整数。If value is present, it is used as the starting value of the checksum; otherwise, a default value of 1 is used. 如果存在value,则将其用作校验和的起始值;否则,将使用默认值1。Passing in value allows computing a running checksum over the concatenation of several inputs. 传入value允许在多个输入的串联上计算运行校验和。The algorithm is not cryptographically strong, and should not be used for authentication or digital signatures. 该算法加密能力不强,不应用于身份验证或数字签名。Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm.由于该算法设计为用作校验和算法,因此不适合用作一般哈希算法。

Changed in version 3.0:版本3.0中更改: The result is always unsigned. 结果总是无符号的。To generate the same numeric value when using Python 2 or earlier, use adler32(data) & 0xffffffff.要在使用Python 2或更早版本时生成相同的数值,请使用adler32(data) & 0xffffffff

zlib.compress(data, /, level=- 1)

Compresses the bytes in data, returning a bytes object containing compressed data. 压缩data中的字节,返回包含压缩数据的字节对象。level is an integer from 0 to 9 or -1 controlling the level of compression; 1 (Z_BEST_SPEED) is fastest and produces the least compression, 9 (Z_BEST_COMPRESSION) is slowest and produces the most. 0 (Z_NO_COMPRESSION) is no compression. level是一个从0到9或-1的整数,控制压缩级别;1(Z_BEST_SPEED)最快且产生的压缩最少,9(Z_BEST_compression)最慢且产生的压缩最多。0(Z_NO_COMPRESSION)为无压缩。The default value is -1 (Z_DEFAULT_COMPRESSION). 默认值为-1(Z_default_COMPRESSION)。Z_DEFAULT_COMPRESSION represents a default compromise between speed and compression (currently equivalent to level 6). Z_DEFAULT_COMPRESSION表示速度和压缩之间的默认折衷(目前相当于6级)。Raises the error exception if any error occurs.如果发生任何error,则引发错误异常。

Changed in version 3.6:版本3.6中更改: level can now be used as a keyword parameter.level现在可以用作关键字参数。

zlib.compressobj(level=-1, method=DEFLATED, wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=Z_DEFAULT_STRATEGY[, zdict])

Returns a compression object, to be used for compressing data streams that won’t fit into memory at once.返回压缩对象,用于压缩无法立即放入内存的数据流。

level is the compression level – an integer from 0 to 9 or -1. level是压缩级别-从09-1的整数。A value of 1 (Z_BEST_SPEED) is fastest and produces the least compression, while a value of 9 (Z_BEST_COMPRESSION) is slowest and produces the most. 1(Z_BEST_SPEED)最快,产生的压缩最少,而值9(Z_BEST_compression)最慢,产生的压缩最多。0 (Z_NO_COMPRESSION) is no compression. 0(Z_NO_COMPRESSION)为无压缩。The default value is -1 (Z_DEFAULT_COMPRESSION). 默认值为-1(Z_default_COMPRESSION)。Z_DEFAULT_COMPRESSION represents a default compromise between speed and compression (currently equivalent to level 6).Z_DEFAULT_COMPRESSION表示速度和压缩之间的默认折衷(目前相当于6级)。

method is the compression algorithm. method是压缩算法。Currently, the only supported value is DEFLATED.目前,唯一支持的值是DEFLATED

The wbits argument controls the size of the history buffer (or the “window size”) used when compressing data, and whether a header and trailer is included in the output. wbits参数控制压缩数据时使用的历史缓冲区的大小(或“窗口大小”),以及输出中是否包含头和尾。It can take several ranges of values, defaulting to 15 (MAX_WBITS):它可以接受多个值范围,默认为15(MAX_WBITS):

  • +9 to +15: The base-two logarithm of the window size, which therefore ranges between 512 and 32768. :窗口大小的以二为底的对数,因此其范围介于512和32768之间。Larger values produce better compression at the expense of greater memory usage. 值越大,压缩效果越好,但内存使用率越高。The resulting output will include a zlib-specific header and trailer.结果输出将包括zlib特定的头和尾。

  • −9 to −15: Uses the absolute value of wbits as the window size logarithm, while producing a raw output stream with no header or trailing checksum.9到15:使用wbits的绝对值作为窗口大小对数,同时生成没有标头或尾部校验和的原始输出流。

  • +25 to +31 = 16 + (9 to 15): Uses the low 4 bits of the value as the window size logarithm, while including a basic gzip header and trailing checksum in the output.+25到+31=16+(9到15):使用值的低4位作为窗口大小对数,同时在输出中包括基本gzip头和尾随校验和。

The memLevel argument controls the amount of memory used for the internal compression state. memLevel参数控制用于内部压缩状态的内存量。Valid values range from 1 to 9. 有效值范围为19Higher values use more memory, but are faster and produce smaller output.值越高,占用的内存越多,但速度越快,输出越小。

strategy is used to tune the compression algorithm. strategy用于调整压缩算法。Possible values are Z_DEFAULT_STRATEGY, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE (zlib 1.2.0.1) and Z_FIXED (zlib 1.2.2.2).可能的值有Z_DEFAULT_STRATEGYZ_FILTEREDZ_HUFFMAN_ONLYZ_RLE(zlib 1.2.0.1)和Z_FIXED(zlib 1.2.2.2)。

zdict is a predefined compression dictionary. 是预定义的压缩字典。This is a sequence of bytes (such as a bytes object) containing subsequences that are expected to occur frequently in the data that is to be compressed. 这是一个字节序列(例如bytes对象),其中包含预期将在要压缩的数据中频繁出现的子序列。Those subsequences that are expected to be most common should come at the end of the dictionary.那些最常见的子序列应该出现在字典的末尾。

Changed in version 3.3:版本3.3中更改: Added the zdict parameter and keyword argument support.添加了zdict参数和关键字参数支持。

zlib.crc32(data[, value])

Computes a CRC (Cyclic Redundancy Check) checksum of data. 计算data的CRC(循环冗余校验)校验和。The result is an unsigned 32-bit integer. 结果是一个无符号32位整数。If value is present, it is used as the starting value of the checksum; otherwise, a default value of 0 is used. 如果存在value,则将其用作校验和的起始值;否则,将使用默认值0。Passing in value allows computing a running checksum over the concatenation of several inputs. 传入value允许在多个输入的串联上计算运行校验和。The algorithm is not cryptographically strong, and should not be used for authentication or digital signatures. 该算法加密能力不强,不应用于身份验证或数字签名。Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm.由于该算法设计为用作校验和算法,因此不适合用作一般哈希算法。

Changed in version 3.0:版本3.0中更改: The result is always unsigned. 结果总是无符号的。To generate the same numeric value when using Python 2 or earlier, use crc32(data) & 0xffffffff.要在使用Python 2或更早版本时生成相同的数值,请使用crc32(data) & 0xffffffff

zlib.decompress(data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)

Decompresses the bytes in data, returning a bytes object containing the uncompressed data. 解压缩data中的字节,返回包含未压缩数据的bytes对象。The wbits parameter depends on the format of data, and is discussed further below. wbits参数取决于data的格式,下面将进一步讨论。If bufsize is given, it is used as the initial size of the output buffer. 如果给定bufsize,则将其用作输出缓冲区的初始大小。Raises the error exception if any error occurs.如果发生任何错误,则引发error异常。

The wbits parameter controls the size of the history buffer (or “window size”), and what header and trailer format is expected. wbits参数控制历史缓冲区的大小(或“窗口大小”),以及预期的头和尾格式。It is similar to the parameter for compressobj(), but accepts more ranges of values:它类似于compressobj()的参数,但接受更多的值范围:

  • +8 to +15: The base-two logarithm of the window size. 窗口大小的以二为底的对数。The input must include a zlib header and trailer.输入必须包括zlib标头和尾部。

  • 0: Automatically determine the window size from the zlib header. 从zlib标头自动确定窗口大小。Only supported since zlib 1.2.3.5.仅在zlib 1.2.3.5之后支持。

  • −8 to −15: Uses the absolute value of wbits as the window size logarithm. 使用wbits的绝对值作为窗口大小的对数。The input must be a raw stream with no header or trailer.输入必须是没有头或尾的原始流。

  • +24 to +31 = 16 + (8 to 15): Uses the low 4 bits of the value as the window size logarithm. 使用该值的低位4位作为窗口大小对数。The input must include a gzip header and trailer.输入必须包括gzip头和尾。

  • +40 to +47 = 32 + (8 to 15): Uses the low 4 bits of the value as the window size logarithm, and automatically accepts either the zlib or gzip format.使用该值的低位4位作为窗口大小对数,并自动接受zlib或gzip格式。

When decompressing a stream, the window size must not be smaller than the size originally used to compress the stream; using a too-small value may result in an error exception. 解压缩流时,窗口大小不得小于最初用于压缩流的大小;使用太小的值可能会导致error异常。The default wbits value corresponds to the largest window size and requires a zlib header and trailer to be included.默认wbits值对应于最大窗口大小,需要包括zlib头和尾。

bufsize is the initial size of the buffer used to hold decompressed data. bufsize是用于保存解压缩数据的缓冲区的初始大小。If more space is required, the buffer size will be increased as needed, so you don’t have to get this value exactly right; tuning it will only save a few calls to malloc().如果需要更多的空间,缓冲区大小将根据需要增加,因此您不必完全正确地获得该值;调整它只会节省对malloc()的一些调用。

Changed in version 3.6:版本3.6中更改: wbits and bufsize can be used as keyword arguments.wbitsbufsize可以用作关键字参数。

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

Returns a decompression object, to be used for decompressing data streams that won’t fit into memory at once.返回一个解压缩对象,用于解压缩无法立即放入内存的数据流。

The wbits parameter controls the size of the history buffer (or the “window size”), and what header and trailer format is expected. wbits参数控制历史缓冲区的大小(或“窗口大小”),以及预期的头和尾格式。It has the same meaning as described for decompress().其含义与decompress()中描述的含义相同。

The zdict parameter specifies a predefined compression dictionary. zdict参数指定预定义的压缩字典。If provided, this must be the same dictionary as was used by the compressor that produced the data that is to be decompressed.如果提供,则该字典必须与生成要解压缩的数据的压缩器使用的字典相同。

Note

If zdict is a mutable object (such as a bytearray), you must not modify its contents between the call to decompressobj() and the first call to the decompressor’s decompress() method.如果zdict是一个可变对象(例如bytearray),则在调用decompressobj()和第一次调用解压器的decompress()方法之间,不能修改其内容。

Changed in version 3.3:版本3.3中更改: Added the zdict parameter.添加了zdict参数。

Compression objects support the following methods:压缩对象支持以下方法:

Compress.compress(data)

Compress data, returning a bytes object containing compressed data for at least part of the data in data. 压缩data,返回包含data中至少部分数据的压缩数据的bytes对象。This data should be concatenated to the output produced by any preceding calls to the compress() method. 该数据应连接到之前对compress()方法的任何调用所产生的输出。Some input may be kept in internal buffers for later processing.一些输入可能保存在内部缓冲区中,以供以后处理。

Compress.flush([mode])

All pending input is processed, and a bytes object containing the remaining compressed output is returned. 处理所有挂起的输入,并返回包含其余压缩输出的bytes对象。mode can be selected from the constants Z_NO_FLUSH, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_BLOCK (zlib 1.2.3.4), or Z_FINISH, defaulting to Z_FINISH. mode可以从常数Z_NO_FLUSHZ_PARTIAL_FLUSHZ_SYNC_FLUSHZ_FULL_FLUSHZ_BLOCK(zlib 1.2.3.4)或Z_FINISH中选择,默认为Z_FINISHExcept Z_FINISH, all constants allow compressing further bytestrings of data, while Z_FINISH finishes the compressed stream and prevents compressing any more data. Z_FINISH外,所有常数都允许进一步压缩数据的bytestring,而Z_FINISH完成压缩流并防止压缩更多数据。After calling flush() with mode set to Z_FINISH, the compress() method cannot be called again; the only realistic action is to delete the object.在调用flush()并将mode设置为Z_FINISH后,无法再次调用compress()方法;唯一真实的操作是删除对象。

Compress.copy()

Returns a copy of the compression object. 返回压缩对象的副本。This can be used to efficiently compress a set of data that share a common initial prefix.这可以用于有效压缩共享公共初始前缀的一组数据。

Changed in version 3.8:版本3.8中更改: Added copy.copy() and copy.deepcopy() support to compression objects.添加了对压缩对象的copy.copy()copy.deepcopy()支持。

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. 一个bytes对象,包含压缩数据末尾之后的任何字节。That is, this remains b"" until the last byte that contains compression data is available. 也就是说,在包含压缩数据的最后一个字节可用之前,这将保持b""If the whole bytestring turned out to contain compressed data, this is b"", an empty bytes object.如果整个ByTest环包含压缩数据,则这是b"",一个空字节对象。

Decompress.unconsumed_tail

A bytes object that contains any data that was not consumed by the last decompress() call because it exceeded the limit for the uncompressed data buffer. 一个bytes对象,其中包含上次decompress()调用未使用的任何数据,因为它超过了未压缩数据缓冲区的限制。This data has not yet been seen by the zlib machinery, so you must feed it (possibly with further data concatenated to it) back to a subsequent decompress() method call in order to get correct output.zlib机器尚未看到这些数据,因此必须将其(可能还有更多连接到其中的数据)反馈给后续的decompress()方法调用,以获得正确的输出。

Decompress.eof

A boolean indicating whether the end of the compressed data stream has been reached.一个布尔值,指示是否已到达压缩数据流的末尾。

This makes it possible to distinguish between a properly-formed compressed stream, and an incomplete or truncated one.这使得能够区分格式正确的压缩流和不完整或被截断的压缩流。

New in version 3.3.版本3.3中新增。

Decompress.decompress(data, max_length=0)

Decompress data, returning a bytes object containing the uncompressed data corresponding to at least part of the data in string. 解压缩data,返回一个字节对象,该对象包含对应于string中至少部分数据的未压缩数据。This data should be concatenated to the output produced by any preceding calls to the decompress() method. 该数据应连接到任何之前对decompress()方法的调用所产生的输出。Some of the input data may be preserved in internal buffers for later processing.一些输入数据可能会保存在内部缓冲区中,以供以后处理。

If the optional parameter max_length is non-zero then the return value will be no longer than max_length. 如果可选参数max_length为非零,则返回值将不大于max_lengthThis may mean that not all of the compressed input can be processed; and unconsumed data will be stored in the attribute unconsumed_tail. 这可能意味着并非所有的压缩输入都可以处理;未使用的数据将存储在属性unconsumed_tail中。This bytestring must be passed to a subsequent call to decompress() if decompression is to continue. 如果要继续解压缩,则必须将此bytestring传递给对decompress()的后续调用。If max_length is zero then the whole input is decompressed, and unconsumed_tail is empty.如果max_length为零,则整个输入被解压缩,unconsumed_tail为空。

Changed in version 3.6:版本3.6中更改: max_length can be used as a keyword argument.max_length可以用作关键字参数。

Decompress.flush([length])

All pending input is processed, and a bytes object containing the remaining uncompressed output is returned. 处理所有挂起的输入,并返回包含其余未压缩输出的bytes对象。After calling flush(), the decompress() method cannot be called again; the only realistic action is to delete the object.调用flush()后,无法再次调用decompress()方法;唯一真实的操作是删除对象。

The optional parameter length sets the initial size of the output buffer.可选参数length设置输出缓冲区的初始大小。

Decompress.copy()

Returns a copy of the decompression object. 返回解压缩对象的副本。This can be used to save the state of the decompressor midway through the data stream in order to speed up random seeks into the stream at a future point.这可以用于在数据流的中途保存解压缩器的状态,以便在将来的某个点加快对数据流的随机搜索。

Changed in version 3.8:版本3.8中更改: Added copy.copy() and copy.deepcopy() support to decompression objects.添加了copy.copy()copy.deepcopy()对解压缩对象的支持。

Information about the version of the zlib library in use is available through the following constants:有关正在使用的zlib库版本的信息可通过以下常量获得:

zlib.ZLIB_VERSION

The version string of the zlib library that was used for building the module. 用于构建模块的zlib库的版本字符串。This may be different from the zlib library actually used at runtime, which is available as ZLIB_RUNTIME_VERSION.这可能不同于在运行时实际使用的zlib库,后者作为ZLIB_RUNTIME_VERSION本提供。

zlib.ZLIB_RUNTIME_VERSION

The version string of the zlib library actually loaded by the interpreter.解释器实际加载的zlib库的版本字符串。

New in version 3.3.版本3.3中新增。

See also另请参见

Module gzip

Reading and writing gzip-format files.读取和写入gzip格式文件。

http://www.zlib.net

The zlib library home page.zlib图书馆主页。

http://www.zlib.net/manual.html

The zlib manual explains the semantics and usage of the library’s many functions.zlib手册解释了库的许多功能的语义和用法。