python利用gzip压缩解压缩StringIO
来源:互联网 发布:好听的编程项目名称 编辑:程序博客网 时间:2024/06/09 15:08
When working with a data stream instead of a file, use the GzipFileclass directly to compress or uncompress it. This is useful when thedata is being transmitted over a socket or from read an existing(already open) file handle. A StringIO buffer can also be used.
Note
When re-reading the previously compressed data, I pass an explicit length toread(). Leaving the length off resulted in a CRC error, possibly becauseStringIO returned an empty string before reporting EOF. If you areworking with streams of compressed data, you may want to prefix the data withan integer representing the actual amount of data to be read.
我按照他的方法,自己写了一下:
>>> import gzip
>>> from cStringIO import StringIO
>>> puredata = 'test'
>>> buf=StringIO()
>>> f=gzip.GzipFile(mode="wb", fileobj=buf)
>>> f.write(puredata)
4
>>> f.close()
>>> cdata = buf.getvalue()
>>> print cdata
ヒ
>>> print len(cdata)
24
>>> import binascii
>>> print binascii.hexlify(cdata)
1f8b0800e0a3ab4f02ff2b492d2e01000c7e7fd804000000
>>> inbuffer = StringIO(cdata)
>>> f = gzip.GzipFile(mode="rb", fileobj=inbuffer)
>>> rdata = f.read()
>>> print rdata
test
成功
之前压缩完了之后总是解压缩不成,提示:
IOError: CRC check failed 0xab380008L != 0x0L
后来发现没有调用f.close(),加上之后就好了
- python利用gzip压缩解压缩StringIO
- python利用gzip压缩解压缩StringIO
- python开发——gzip压缩|解压缩
- python webservice gzip压缩与解压缩
- gzip, StringIO的压缩和解压
- Gzip压缩解压缩
- php gzip压缩解压缩
- gzip压缩解压缩数据
- Zip压缩、解压缩方法(GZip)
- java Gzip 压缩与解压缩
- GZip压缩解压缩工具类
- GZIP/ZIP压缩与解压缩
- java Gzip 压缩与解压缩
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- php 操作 com1口
- zz 人脸识别发展史与算法综述
- 多事之秋
- Silverlight/Windows8/WPF/WP7/HTML5周学习导读(8月5日-8月12日)
- 如何在Django中接收JSON格式的数据
- python利用gzip压缩解压缩StringIO
- Python mechanize gzip response handling
- 高新技术之代理
- [转自豆瓣~]C,C++,C#三者区别
- opencv下调用K均值函数cvKMeans2聚类图像例程
- poj 1066 计算几何构图+最短路
- Oracle RBA 浅谈
- Swing中JInternalFrame窗口平铺、级联等实现
- mac体验具体步骤详解