python 模块zlib 压缩与解压
例子1:压缩与解压字符串
import zlib
message = 'abcd1234'
compressed = zlib.compress(message)
decompressed = zlib.decompress(compressed) print 'original:', repr(message)
print 'compressed:', repr(compressed)
print 'decompressed:', repr(decompressed)
结果
original: 'abcd1234'
compressed: 'x\x9cKLJN1426\x01\x00\x0b\xf8\x02U'
decompressed: 'abcd1234'
例子2:压缩与解压文件
import 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()) if __name__ == "__main__":
compress('in.txt', 'out.txt')
decompress('out.txt', 'out_decompress.txt')
结果
生成文件
out_decompress.txt out.txt
注:
zlib.compress用于压缩流数据。参数string指定了要压缩的数据流,参数level指定了压缩的级别,它的取值范围是1到9。压缩速度与压缩率成反比,1表示压缩速度最快,而压缩率最低,而9则表示压缩速度最慢但压缩率最高
问题——处理对象过大异常
>>> import zlib
>>> a = ''
>>> b = zlib.compress(a)
>>> b
'x\x9c342\x06\x00\x01-\x00\x97'
>>> a = 'a' * * * *
>>> b = zlib.compress(a)
Traceback (most recent call last):
File "<stdin>", line , in <module>
OverflowError: size does not fit in an int
python 模块zlib 压缩与解压的更多相关文章
- python用模块zlib压缩与解压字符串和文件的方法
摘自:http://www.jb51.net/article/100218.htm Python标准模块中,有多个模块用于数据的压缩与解压缩,如zipfile,gzip, bz2等等. python中 ...
- 浅谈在c#中使用Zlib压缩与解压的方法
作者:Compasslg 介绍 近期用c#开发一个游戏的存档编辑工具需要用 Zlib 标准的 Deflate 算法对数据进行解压. 在 StackOverflow 上逛了一圈,发现 c# 比较常用到的 ...
- Delphi Base64编码_解码及ZLib压缩_解压(转)
最近在写的程序与SOAP相关,所以用到了一些Base64编码/解码及数据压缩/解压方面的知识. 在这里来作一些总结:一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当然 ...
- python的shutil模块-文件的移动、复制、打包、压缩、解压等
参考https://www.cnblogs.com/xiangsikai/p/7787101.html os模块提供了对目录或者文件的新建.删除.查看文件属性,还提供了对文件以及目录的路径操作,比如说 ...
- 模块讲解----shutil模块(copy、压缩、解压)
作用与功能 主要用于文件的copy,压缩,解压 导入shuitl模块: import shutil copy方法 1.shutil.copyfileobj() 打开file1,并copy写入file ...
- 常用模块(shutil copy、压缩、解压)
作用与功能 主要用于文件的copy,压缩,解压 导入shuitl模块: import shutil copy方法 1 1.shutil.copyfileobj() 打开file1,并copy写入fi ...
- 【JNI】OPUS压缩与解压的JNI调用(.DLL版本)
OPUS压缩与解压的JNI调用(.DLL版本) 一.写在开头: 理论上讲,这是我在博客园的第一篇原创的博客,之前也一直想找个地方写点东西,把最近做的一些东西归纳总结下,但是一般工程做完了一高兴就把东西 ...
- 自定义DelegatingHandler为ASP.NET Web Api添加压缩与解压的功能
HTTP协议中的压缩 Http协议中使用Accept-Encoding和Content-Encoding头来表示期望Response内容的编码和当前Request的内容编码.而Http内容的压缩其实是 ...
- 【转】iOS开发之压缩与解压文件
ziparchive是基于开源代码”MiniZip”的zip压缩与解压的Objective-C 的Class,使用起来非常的简单方法:从http://code.google.com/p/ziparch ...
随机推荐
- Asp.Net:Repeater 详情 备用
页面 repeator就想for循环一样,没有编辑模板,有删除delete和详情detail模板 <%@ Page Language="C#" AutoEventWireup ...
- ZOJ 2974 矩阵快速幂
题意 给出n个杯子与初始其中有多少水 “同时”进行如下指令 将其中的水同时分入所指定的杯子 进行x次后 输出杯子剩余水量 队友想出应该是一道快速幂 但并不是过去的用初始杯子的水组成的矩阵乘某个矩阵 可 ...
- Vertex Fetch Texture (VTF)
http://www.opengl.org/wiki/Vertex_Texture_Fetch Vertex Texture Fetch This article contains inacc ...
- 【IOS笔记】Using View Controllers in Your App
参考:http://www.cnblogs.com/patientAndPersist/p/3279645.html Using View Controllers in Your App Whethe ...
- HTML: xhtml和html的區別
XHTML 与 HTML 4.01 标准没有太多的不同. 另外,你应该马上使用小写字母编写HTML代码,同时绝不要养成忽略类似 </p> 标签的坏习惯. 最主要的不同: XHTML 元素必 ...
- 控制变量法-初中物理-Nobel Lecture, December 12, 1929-php执行SET GLOBAL connect_timeout=2效果
$link = mysqli_connect("localhost", "wu", "wp", "wdb"); $sql ...
- routing decisions based on paths, network policies, or rule-sets configured by a network administrator
https://en.wikipedia.org/wiki/Border_Gateway_Protocol Border Gateway Protocol (BGP) is a standardize ...
- Andrew Ng机器学习公开课笔记 -- Mixtures of Gaussians and the EM algorithm
网易公开课,第12,13课 notes,7a, 7b,8 从这章开始,介绍无监督的算法 对于无监督,当然首先想到k means, 最典型也最简单,有需要直接看7a的讲义 Mixtures of G ...
- quanpailie quanbianli
#include<stdio.h>char data[5]={'a','b','c','d','e'};int vist[5]={0};char step[5]={0};char bu[5 ...
- Python - 素数筛
def shai(n): prim = list() check = list([1] * (n + 1)) for i in range(2, n + 1): if(check[i] == 1): ...