gzip --- 對 gzip 格式的支持?

源代碼: Lib/gzip.py


此模塊提供的簡單接口幫助用戶壓縮和解壓縮文件,功能類似于 GNU 應(yīng)用程序 gzipgunzip

數(shù)據(jù)壓縮由 zlib 模塊提供。

gzip 模塊提供 GzipFile 類和 open()compress()decompress() 幾個便利的函數(shù)。GzipFile 類可以讀寫 gzip 格式的文件,還能自動壓縮和解壓縮數(shù)據(jù),這讓操作壓縮文件如同操作普通的 file object 一樣方便。

注意,此模塊不支持部分可以被 gzipgunzip 解壓的格式,如利用 compresspack 壓縮所得的文件。

這個模塊定義了以下內(nèi)容:

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

以二進(jìn)制方式或者文本方式打開一個 gzip 格式的壓縮文件,返回一個 file object

filename 參數(shù)可以是一個實際的文件名(一個a str 對象或者 bytes 對象), 或者是一個用來讀寫的已存在的文件對象。

mode 參數(shù)可以是二進(jìn)制模式: 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' , 或者是文本模式 'rt', 'at', 'wt', or 'xt'。默認(rèn)值是 'rb'

The compresslevel argument is an integer from 0 to 9, as for the GzipFile constructor.

對于二進(jìn)制模式,這個函數(shù)等價于 GzipFile 構(gòu)造器:GzipFile(filename, mode, compresslevel)。在這個例子中,encoding, errorsnewline 三個參數(shù)一定不要設(shè)置。

對于文本模式,將會創(chuàng)建一個 GzipFile 對象,并將它封裝到一個 io.TextIOWrapper 實例中, 這個實例默認(rèn)了指定編碼,錯誤抓獲行為和行。

在 3.3 版更改: 支持 filename 為一個文件對象,支持文本模式和 encoding, errorsnewline 參數(shù)。

在 3.4 版更改: 支持 'x', 'xb' 和``'xt'`` 三種模式。

在 3.6 版更改: 接受一個 path-like object

class gzip.GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)?

GzipFile 類的構(gòu)造器支持 truncate() 的異常,與 file object 的大多數(shù)方法非常相似。fileobjfilename 至少有一個不為空。

新的實例基于 fileobj,它可以是一個普通文件,一個 io.BytesIO 對象,或者任何一個與文件相似的對象。當(dāng) filename 是一個文件對象時,它的默認(rèn)值是 None

當(dāng) fileobjNone 時, filename 參數(shù)只用于 gzip 文件頭中,這個文件有可能包含未壓縮文件的源文件名。如果文件可以被識別,默認(rèn) fileobj 的文件名;否則默認(rèn)為空字符串,在這種情況下文件頭將不包含源文件名。

The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or 'xb', depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is 'rb'.

需要注意的是,文件默認(rèn)使用二進(jìn)制模式打開。如果要以文本模式打開文件一個壓縮文件,請使用 open() 方法(或者使用 io.TextIOWrapper 包裝 GzipFile )。

compresslevel 參數(shù)是一個從 0 to 9 的整數(shù),用于控制壓縮等級;1 最快但壓縮比例最小,9 最慢但壓縮比例最大。 0 不壓縮。默認(rèn)為 9

The mtime argument is an optional numeric timestamp to be written to the last modification time field in the stream when compressing. It should only be provided in compression mode. If omitted or None, the current time is used. See the mtime attribute for more details.

調(diào)用 GzipFileclose() 方法不會關(guān)閉 fileobj,因為你可以希望增加其它內(nèi)容到已經(jīng)壓縮的數(shù)中。你可以將一個 io.BytesIO 對象作為 fileobj,也可以使用 io.BytesIOgetvalue() 方法從內(nèi)存緩存中恢復(fù)數(shù)據(jù)。

GzipFile 支持 io.BufferedIOBase 類的接口, 包括迭代和 with 語句。只有 truncate() 方法沒有實現(xiàn)。

GzipFile 還提供了以下的方法和屬性:

peek(n)?

在不移動文件指針的情況下讀取 n 個未壓縮字節(jié)。最多只有一個單獨的讀取流來服務(wù)這個方法調(diào)用。返回的字節(jié)數(shù)不一定剛好等于要求的數(shù)量。

注解

調(diào)用 peek() 并沒有改變 GzipFile 的文件指針,它可能改變潛在文件對象(例如: GzipFile 使用 fileobj 參數(shù)進(jìn)行初始化)。

3.2 新版功能.

mtime?

在解壓的過程中,最后修改時間字段的值可能來自于這個屬性,以整數(shù)的形式出現(xiàn)。在讀取任何文件頭信息前,初始值為 None

所有 gzip 東方壓縮流中必須包含時間戳這個字段。以便于像 gunzip這樣的程序可以使用時間戳。格式與 time.time() 的返回值和 os.stat() 對象的 st_mtime 屬性值一樣。

在 3.1 版更改: 支持 with 語句,構(gòu)造器參數(shù) mtimemtime 屬性。

在 3.2 版更改: 添加了對零填充和不可搜索文件的支持。

在 3.3 版更改: 實現(xiàn) io.BufferedIOBase.read1() 方法。

在 3.4 版更改: 支持 'x' and 'xb' 兩種模式。

在 3.5 版更改: 支持寫入任意 bytes-like objectsread() 方法可以接受``None``為參數(shù)。

在 3.6 版更改: 接受一個 path-like object

gzip.compress(data, compresslevel=9)?

壓縮 data 返回一個包含壓縮數(shù)據(jù)的 bytes 對象。 compresslevel 的意義與上面提到的 GzipFile 構(gòu)造器一至。

3.2 新版功能.

gzip.decompress(data)?

解壓數(shù)據(jù),返回一個 bytes 包含未解壓數(shù)據(jù)的對象。

3.2 新版功能.

用法示例?

讀取壓縮文件示例:

import gzip
with gzip.open('/home/joe/file.txt.gz', 'rb') as f:
    file_content = f.read()

創(chuàng)建GZIP 文件示例:

import gzip
content = b"Lots of content here"
with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
    f.write(content)

使用 GZIP 壓縮已有的文件示例:

import gzip
import shutil
with open('/home/joe/file.txt', 'rb') as f_in:
    with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

使用 GZIP 壓縮二進(jìn)制字符串示例:

import gzip
s_in = b"Lots of content here"
s_out = gzip.compress(s_in)

參見

模塊 zlib

支持 gzip 格式所需要的基本壓縮模塊。