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ù)可以是一個實(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 實(shí)例中, 這個實(shí)例默認(rèn)了指定編碼,錯誤抓獲行為和行。

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

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

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

exception gzip.BadGzipFile?

針對無效 gzip 文件引發(fā)的異常。 它繼承自 OSError。 針對無效 gzip 文件也可能引發(fā) EOFErrorzlib.error

3.8 新版功能.

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

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

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

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

mode 參數(shù)可以是 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x''xb' 中的一個,具體取決于文件將被讀取還是被寫入。 如果可識別則默認(rèn)為 fileobj 的模式;否則默認(rèn)為 'rb'。 在未來的 Python 發(fā)布版中將不再使用 fileobj 的模式。 最好總是指定 mode 為寫入模式。

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

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

mtime 參數(shù)是一個可選的數(shù)字時間戳用于寫入流的最后修改字段,。mtime 只在壓縮模式中使用。如果省略或者值為 None,則使用當(dāng)前時間。更多細(xì)節(jié),詳見 mtime 屬性。

調(diào)用 GzipFileclose() 方法不會關(guān)閉 fileobj,因?yàn)槟憧梢韵M黾悠渌鼉?nèi)容到已經(jīng)壓縮的數(shù)中。你可以將一個 io.BytesIO 對象作為 fileobj,也可以使用 io.BytesIOgetvalue() 方法從內(nèi)存緩存中恢復(fù)數(shù)據(jù)。

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

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

peek(n)?

在不移動文件指針的情況下讀取 n 個未壓縮字節(jié)。最多只有一個單獨(dú)的讀取流來服務(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 版更改: 實(shí)現(xiàn) io.BufferedIOBase.read1() 方法。

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

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

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

3.9 版后已移除: 打開 GzipFile 用于寫入而不指定 mode 參數(shù)的做法已被棄用。

gzip.compress(data, compresslevel=9, *, mtime=None)?

Compress the data, returning a bytes object containing the compressed data. compresslevel and mtime have the same meaning as in the GzipFile constructor above. When mtime is set to 0, this function is equivalent to zlib.compress() with wbits set to 31. The zlib function is faster.

3.2 新版功能.

在 3.8 版更改: 添加了 mtime 形參用于可重復(fù)的輸出。

在 3.11 版更改: Speed is improved by compressing all data at once instead of in a streamed fashion. Calls with mtime set to 0 are delegated to zlib.compress() for better speed.

gzip.decompress(data)?

Decompress the data, returning a bytes object containing the uncompressed data. This function is capable of decompressing multi-member gzip data (multiple gzip blocks concatenated together). When the data is certain to contain only one member the zlib.decompress() function with wbits set to 31 is faster.

3.2 新版功能.

在 3.11 版更改: Speed is improved by decompressing members at once in memory instead of in a streamed fashion.

用法示例?

讀取壓縮文件示例:

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 格式所需要的基本壓縮模塊。

命令行界面?

gzip 模塊提供了簡單的命令行界面用于壓縮和解壓縮文件。

在執(zhí)行后 gzip 模塊會保留輸入文件。

在 3.8 版更改: 添加一個帶有用法說明的新命令行界面命令。 默認(rèn)情況下,當(dāng)你要執(zhí)行 CLI 時,默認(rèn)壓縮等級為 6。

命令行選項(xiàng)?

file?

如果 file 未指定,則從 sys.stdin 讀取。

--fast?

指明最快速的壓縮方法(較低壓縮率)。

--best?

指明最慢速的壓縮方法(最高壓縮率)。

-d, --decompress?

解壓縮給定的文件。

-h, --help?

顯示幫助消息。