gzip — 支持 gzip 文件

源代码: Lib/gzip.py


This module provides a simple interface to compress and decompress files just like the GNU programs gzip and gunzip would.

The data compression is provided by the zlib 模块。

The gzip module provides the GzipFile class, as well as the open() , compress() and decompress() convenience functions. The GzipFile class reads and writes gzip -format files, automatically compressing or decompressing the data so that it looks like an ordinary 文件对象 .

Note that additional file formats which can be decompressed by the gzip and gunzip programs, such as those produced by compress and pack , are not supported by this module.

模块定义了下列项:

gzip. open ( filename , mode = 'rb' , compresslevel = 9 , encoding = None , errors = None , newline = None )

Open a gzip-compressed file in binary or text mode, returning a 文件对象 .

The filename argument can be an actual filename (a str or bytes object), or an existing file object to read from or write to.

The mode argument can be any of 'r' , 'rb' , 'a' , 'ab' , 'w' , 'wb' , 'x' or 'xb' 对于二进制模式,或 'rt' , 'at' , 'wt' ,或 'xt' for text mode. The default is 'rb' .

The compresslevel argument is an integer from 0 to 9, as for the GzipFile 构造函数。

For binary mode, this function is equivalent to the GzipFile 构造函数: GzipFile(filename, mode, compresslevel) 。在此情况下, encoding , errors and newline arguments must not be provided.

对于文本模式, GzipFile object is created, and wrapped in an io.TextIOWrapper instance with the specified encoding, error handling behavior, and line ending(s).

3.3 版改变: 添加支持 filename being a file object, support for text mode, and the encoding , errors and newline 自变量。

3.4 版改变: 添加支持 'x' , 'xb' and 'xt' 模式。

3.6 版改变: 接受 像路径对象 .