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

源代碼: Lib/gzip.py


此模塊提供的簡(jiǎn)單接口幫助用戶(hù)壓縮和解壓縮文件,功能類(lèi)似于 GNU 應用程序 gzipgunzip。

數據壓縮由 zlib 模塊提供。

gzip 模塊提供 GzipFile 類(lèi)和 open()、compress()、decompress() 幾個(gè)便利的函數。GzipFile 類(lèi)可以讀寫(xiě) gzip 格式的文件,還能自動(dòng)壓縮和解壓縮數據,這讓操作壓縮文件如同操作普通的 file object 一樣方便。

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

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

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

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

filename 參數可以是一個(gè)實(shí)際的文件名(一個(gè)a str 對象或者 bytes 對象), 或者是一個(gè)用來(lái)讀寫(xiě)的已存在的文件對象。

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

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

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

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

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

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

在 3.6 版更改: 接受一個(gè) path-like object。

exception gzip.BadGzipFile?

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

3.8 新版功能.

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

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

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

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

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

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

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

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

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

GzipFile 支持 io.BufferedIOBase 類(lèi)的接口, 包括迭代和 with 語(yǔ)句。只有 truncate() 方法沒(méi)有實(shí)現。

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

peek(n)?

在不移動(dòng)文件指針的情況下讀取 n 個(gè)未壓縮字節。最多只有一個(gè)單獨的讀取流來(lái)服務(wù)這個(gè)方法調用。返回的字節數不一定剛好等于要求的數量。

備注

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

3.2 新版功能.

mtime?

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

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

在 3.1 版更改: 支持 with 語(yǔ)句,構造器參數 mtimemtime 屬性。

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

在 3.3 版更改: 實(shí)現 io.BufferedIOBase.read1() 方法。

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

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

在 3.6 版更改: 接受一個(gè) path-like object。

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

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 形參用于可重復的輸出。

在 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)

參見(jiàn)

模塊 zlib

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

命令行界面?

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

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

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

命令行選項?

file?

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

--fast?

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

--best?

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

-d, --decompress?

解壓縮給定的文件。

-h, --help?

顯示幫助消息。