在Python中使用zlib模塊進(jìn)行數(shù)據(jù)壓縮的教程
Python標(biāo)準(zhǔn)模塊中,有多個模塊用于數(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指定了壓縮的級別,它的取值范圍是1到9。壓縮速度與壓縮率成反比,1表示壓縮速度最快,而壓縮率最低,而9則表示壓縮速度最慢但壓縮率最高。zlib.decompress用于解壓數(shù)據(jù)。參數(shù)string指定了需要解壓的數(shù)據(jù),wbits和bufsize分別用于設(shè)置系統(tǒng)緩沖區(qū)大小(window buffer )與輸出緩沖區(qū)大小(output buffer)。下面用一個例子來演示如何使用這兩個方法:
#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對象來對數(shù)據(jù)進(jìn)行壓縮/解壓縮。zlib.compressobj([level]) 與zlib.decompress(string[, wbits[, bufsize]]) 分別創(chuàng)建Compress/Decompress縮對象。通過對象對數(shù)據(jù)進(jìn)行壓縮和解壓縮的使用方式與上面介紹的zlib.compress,zlib.decompress非常類似。但兩者對數(shù)據(jù)的壓縮還是有區(qū)別的,這主要體現(xiàn)在對大量數(shù)據(jù)進(jìn)行操作的情況下。假如現(xiàn)在要壓縮一個非常大的數(shù)據(jù)文件(上百M(fèi)),如果使用zlib.compress來壓縮的話,必須先一次性將文件里的數(shù)據(jù)讀到內(nèi)存里,然后將數(shù)據(jù)進(jìn)行壓縮。這樣勢必會戰(zhàn)用太多的內(nèi)存。如果使用對象來進(jìn)行壓縮,那么沒有必要一次性讀取文件的所有數(shù)據(jù),可以先讀一部分?jǐn)?shù)據(jù)到內(nèi)存里進(jìn)行壓縮,壓縮完后寫入文件,然后再讀其他部分的數(shù)據(jù)壓縮,如此循環(huán)重復(fù),只到壓縮完整個文件。下面一個例子來演示這之間的區(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ù)長度:', len(data)
print '-' * 30
print 'zlib.compress壓縮后:', len(str1)
print 'zlib.decompress解壓后:', len(str2)
print '-' * 30
#---- 使用Compress, Decompress對象對數(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對象,對數(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ù)長度: 5783
------------------------------
zlib.compress壓縮后: 1531
zlib.decompress解壓后: 5783
------------------------------
Compress.compress壓縮后: 1531
Decompress.decompress解壓后: 5783
------------------------------
分塊壓縮后: 1531
分塊解壓后: 5783
Python手冊對zlib模塊的介紹比較詳細(xì),更具體的應(yīng)用,可以參考Python手冊。
相關(guān)文章
Python Socket 編程知識點(diǎn)詳細(xì)介紹
這篇文章主要介紹了Python Socket 編程,Socket又稱為套接字,它是所有網(wǎng)絡(luò)通信的基礎(chǔ)。網(wǎng)絡(luò)通信其實(shí)就是進(jìn)程間的通信,Socket主要是使用IP地址,協(xié)議,端口號來標(biāo)識一個進(jìn)程,下文詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-02-02
python 將對象設(shè)置為可迭代的兩種實(shí)現(xiàn)方法
今天小編就為大家分享一篇python 將對象設(shè)置為可迭代的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python爬蟲框架scrapy實(shí)戰(zhàn)之爬取京東商城進(jìn)階篇
這篇文章主要給大家介紹了利用python爬蟲框架scrapy爬取京東商城的相關(guān)資料,文中給出了詳細(xì)的代碼介紹供大家參考學(xué)習(xí),并在文末給出了完整的代碼,需要的朋友們可以參考學(xué)習(xí),下面來一起看看吧。2017-04-04
Python pandas.DataFrame 找出有空值的行
這篇文章主要介紹了Python pandas.DataFrame 找出有空值的行,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
python服務(wù)器與android客戶端socket通信實(shí)例
這篇文章主要介紹了python服務(wù)器與android客戶端socket通信的實(shí)現(xiàn)方法,實(shí)例形式詳細(xì)講述了Python的服務(wù)器端實(shí)現(xiàn)原理與方法,以及對應(yīng)的Android客戶端實(shí)現(xiàn)方法,需要的朋友可以參考下2014-11-11

