3×7=51

做最好的自己

 

【python】使用zlib进行压缩解压

A:如何使用zlib模块来进行压缩解压了?
Q:
1.使用zlib.compress可以压缩字符串。使用zlib.decompress可以解压字符串。
压缩解压字符串示范代码:
import zlib
message = 'witch which has which witches wrist watch'
compressed = zlib.compress(message)
decompressed = zlib.decompress(compressed)
print 'original:', repr(message)
print 'compressed:', repr(compressed)
print 'decompressed:', repr(decompressed)
输出
original: 'witch which has which witches wrist watch'
compressed: 'x\x9c+\xcf,I\xceP(\xcf\xc8\x04\x92\x19\x89\xc5PV9H4\x15\xc8+\xca,.Q(O\x04\xf2\x00D?\x0f\x89'
decompressed: 'witch which has which witches wrist watch'
2.使用zlib.compressobj和zlib.decompressobj比较适合用于对文件进行压缩解压。
以下定义的两个函数可以做为使用zlib.compressobj和zlib.decompressobj对文件进行压缩解压的例子:
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())

posted on 2006-06-20 23:41 3×7=51 阅读(10710) 评论(4)  编辑 收藏 引用 所属分类: Python

评论

# re: 【python】使用zlib进行压缩解压 2006-07-03 17:31 Saniko

你这小子看来蟒蛇吃得不少了吧。  回复  更多评论   

# re: 【python】使用zlib进行压缩解压 2006-08-18 14:15 g

小子用LUA吧~!  回复  更多评论   

# re: 【python】使用zlib进行压缩解压 2006-08-20 19:57 3×7=51

唉,不懂python是不会真正体会到python的好处的。
btw:准备搬家到歪酷博客去了。感觉在这里看着首页上面的文章很不爽。  回复  更多评论   

# re: 【python】使用zlib进行压缩解压 2007-12-15 20:43 小可

种子是什么啊?什么号码啊?能告诉吗  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

常用链接

留言簿(3)

随笔分类

随笔档案

blog

积分与排名

最新评论