在Python中使用zlib模塊進(jìn)行數(shù)據(jù)壓縮的教程
Python標(biāo)準(zhǔn)模塊中,有多個(gè)模塊用于數(shù)據(jù)的壓縮與解壓縮,如zipfile,gzip, bz2等等。上次介紹了zipfile模塊,今天就來講講zlib模塊。
zlib.compress(string[, level])
zlib.decompress(string[, wbits[, bufsize]])
zlib.compress用于壓縮流數(shù)據(jù)。參數(shù)string指定了要壓縮的數(shù)據(jù)流,參數(shù)level指定了壓縮的級(jí)別,它的取值范圍是1到9。壓縮速度與壓縮率成反比,1表示壓縮速度最快,而壓縮率最低,而9則表示壓縮速度最慢但壓縮率最高。zlib.decompress用于解壓數(shù)據(jù)。參數(shù)string指定了需要解壓的數(shù)據(jù),wbits和bufsize分別用于設(shè)置系統(tǒng)緩沖區(qū)大小(window buffer )與輸出緩沖區(qū)大小(output buffer)。下面用一個(gè)例子來演示如何使用這兩個(gè)方法:
#coding=gbk import zlib, urllib fp = urllib.urlopen('http://localhost/default.html') str = fp.read() fp.close() #---- 壓縮數(shù)據(jù)流。 str1 = zlib.compress(str, zlib.Z_BEST_COMPRESSION) str2 = zlib.decompress(str1) print len(str) print len(str1) print len(str2) # ---- 結(jié)果 #5783 #1531 #5783
我們也可以使用Compress/Decompress對(duì)象來對(duì)數(shù)據(jù)進(jìn)行壓縮/解壓縮。zlib.compressobj([level]) 與zlib.decompress(string[, wbits[, bufsize]]) 分別創(chuàng)建Compress/Decompress縮對(duì)象。通過對(duì)象對(duì)數(shù)據(jù)進(jìn)行壓縮和解壓縮的使用方式與上面介紹的zlib.compress,zlib.decompress非常類似。但兩者對(duì)數(shù)據(jù)的壓縮還是有區(qū)別的,這主要體現(xiàn)在對(duì)大量數(shù)據(jù)進(jìn)行操作的情況下。假如現(xiàn)在要壓縮一個(gè)非常大的數(shù)據(jù)文件(上百M(fèi)),如果使用zlib.compress來壓縮的話,必須先一次性將文件里的數(shù)據(jù)讀到內(nèi)存里,然后將數(shù)據(jù)進(jìn)行壓縮。這樣勢(shì)必會(huì)戰(zhàn)用太多的內(nèi)存。如果使用對(duì)象來進(jìn)行壓縮,那么沒有必要一次性讀取文件的所有數(shù)據(jù),可以先讀一部分?jǐn)?shù)據(jù)到內(nèi)存里進(jìn)行壓縮,壓縮完后寫入文件,然后再讀其他部分的數(shù)據(jù)壓縮,如此循環(huán)重復(fù),只到壓縮完整個(gè)文件。下面一個(gè)例子來演示這之間的區(qū)別:
#coding=gbk import zlib, urllib fp = urllib.urlopen('http://localhost/default.html') # 訪問的到的網(wǎng)址。 data = fp.read() fp.close() #---- 壓縮數(shù)據(jù)流 str1 = zlib.compress(data, zlib.Z_BEST_COMPRESSION) str2 = zlib.decompress(str1) print '原始數(shù)據(jù)長(zhǎng)度:', len(data) print '-' * 30 print 'zlib.compress壓縮后:', len(str1) print 'zlib.decompress解壓后:', len(str2) print '-' * 30 #---- 使用Compress, Decompress對(duì)象對(duì)數(shù)據(jù)流進(jìn)行壓縮/解壓縮 com_obj = zlib.compressobj(zlib.Z_BEST_COMPRESSION) decom_obj = zlib.decompressobj() str_obj = com_obj.compress(data) str_obj += com_obj.flush() print 'Compress.compress壓縮后:', len(str_obj) str_obj1 = decom_obj.decompress(str_obj) str_obj1 += decom_obj.flush() print 'Decompress.decompress解壓后:', len(str_obj1) print '-' * 30 #---- 使用Compress, Decompress對(duì)象,對(duì)數(shù)據(jù)進(jìn)行分塊壓縮/解壓縮。 com_obj1 = zlib.compressobj(zlib.Z_BEST_COMPRESSION) decom_obj1 = zlib.decompressobj() chunk_size = 30; #原始數(shù)據(jù)分塊 str_chunks = [data[i * chunk_size:(i + 1) * chunk_size] / for i in range((len(data) + chunk_size) / chunk_size)] str_obj2 = '' for chunk in str_chunks: str_obj2 += com_obj1.compress(chunk) str_obj2 += com_obj1.flush() print '分塊壓縮后:', len(str_obj2) #壓縮數(shù)據(jù)分塊解壓 str_chunks = [str_obj2[i * chunk_size:(i + 1) * chunk_size] / for i in range((len(str_obj2) + chunk_size) / chunk_size)] str_obj2 = '' for chunk in str_chunks: str_obj2 += decom_obj1.decompress(chunk) str_obj2 += decom_obj1.flush() print '分塊解壓后:', len(str_obj2) # ---- 結(jié)果 ------------------------ 原始數(shù)據(jù)長(zhǎng)度: 5783 ------------------------------ zlib.compress壓縮后: 1531 zlib.decompress解壓后: 5783 ------------------------------ Compress.compress壓縮后: 1531 Decompress.decompress解壓后: 5783 ------------------------------ 分塊壓縮后: 1531 分塊解壓后: 5783
Python手冊(cè)對(duì)zlib模塊的介紹比較詳細(xì),更具體的應(yīng)用,可以參考Python手冊(cè)。
相關(guān)文章
python數(shù)學(xué)模塊(math/decimal模塊)
這篇文章主要介紹了python數(shù)學(xué)模塊(math/decimal模塊),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Python使用20行代碼實(shí)現(xiàn)微信聊天機(jī)器人
這篇文章主要介紹了Python使用20行代碼實(shí)現(xiàn)微信聊天機(jī)器人,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06利用 Python ElementTree 生成 xml的實(shí)例
這篇文章主要介紹了利用 Python ElementTree 生成 xml的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python中內(nèi)建函數(shù)的簡(jiǎn)單用法說明
這篇文章主要介紹了Python中內(nèi)建函數(shù)的簡(jiǎn)單用法說明,包括apply()、filter()、reduce()、map()四個(gè)函數(shù)的用法講解,需要的朋友可以參考下2016-05-05Python Metaclass原理與實(shí)現(xiàn)過程詳細(xì)講解
MetaClass元類,本質(zhì)也是一個(gè)類,但和普通類的用法不同,它可以對(duì)類內(nèi)部的定義(包括類屬性和類方法)進(jìn)行動(dòng)態(tài)的修改??梢赃@么說,使用元類的主要目的就是為了實(shí)現(xiàn)在創(chuàng)建類時(shí),能夠動(dòng)態(tài)地改變類中定義的屬性或者方法2022-11-11解決運(yùn)行django程序出錯(cuò)問題 ''str''object has no attribute''_meta''
這篇文章主要介紹了解決運(yùn)行django程序出錯(cuò)問題 'str'object has no attribute'_meta',具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07pytorch中permute()函數(shù)用法實(shí)例詳解
permute中參數(shù)為張量的維度,將不同維度以不同的維度排列,得到一個(gè)新的張量,在深度學(xué)習(xí)中的主要作用是將特征值聚類,下面這篇文章主要給大家介紹了關(guān)于pytorch中permute()函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2022-04-04