PythonTip >> 博文 >> python

python zlib库

zihua 2014-01-20 23:01:15 点击: 721 | 收藏


import zlib
message = 'aaaabbbbccccdddd'
compressed = zlib.compress(message) 
decompressed = zlib.decompress(compressed) 
print 'original:', repr(message) 
print 'compressed:', repr(compressed) 
print 'decompressed:', repr(decompressed)

1.2 使用zlib解压缩文件

def compress(infile, dst, level=9):
    infile = open(infile, 'rb')
    dst = open(dst, 'wb')
    compress = zlib.compressobj(level)
    data = infile.read(1024)
    while data:
        dst.write(compress.compress(data))
        data = infile.read(1024)
    dst.write(compress.flush())
def decompress(infile, dst):
    infile = open(infile, 'rb')
    dst = open(dst, 'wb')
    decompress = zlib.decompressobj()
    data = infile.read(1024)
    while data:
        dst.write(decompress.decompress(data))
        data = infile.read(1024)
    dst.write(decompress.flush())
原文链接:http://www.wumii.com/item/1BSKpCpJ

作者:zihua | 分类: python | 标签: python | 阅读: 721 | 发布于: 2014-01-20 23时 |