利用python中的gzip模块压缩和解压数据流和文件

时间:2021-12-28 03:57:11

直接给出源码实现, 分为两种情况:

1.网络连接中的数据流的压缩和解压,或是打开的文件读取一部分

2.打开文件压缩或是解压

#!/usr/bin/env python
#encoding: utf-8
#filename: gzip_demo.py
#author: tao_627@aliyun.com
#date: 2015-06-30

import gzip, binascii, os
from cStringIO import StringIO

def gzip_compress(raw_data):
buf = StringIO()
f = gzip.GzipFile(mode='wb', fileobj=buf)
try:
f.write(raw_data)
finally:
f.close()
return buf.getvalue()

def gzip_uncompress(c_data):
buf = StringIO(c_data)
f = gzip.GzipFile(mode = 'rb', fileobj = buf)
try:
r_data = f.read()
finally:
f.close()
return r_data


def compress_file(fn_in, fn_out):
f_in = open(fn_in, 'rb')
f_out = gzip.open(fn_out, 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()

def uncompress_file(fn_in, fn_out):
f_in = gzip.open(fn_in, 'rb')
f_out = open(fn_out, 'wb')
file_content = f_in.read()
f_out.write(file_content)
f_out.close()
f_in.close()


if __name__ == '__main__':
in_data = 'hello, world!'
print in_data
out_data = gzip_compress(in_data)
print binascii.hexlify(out_data)

r_data = gzip_uncompress(out_data)
print r_data

raw_f = '/opt/log/raw/access.log_HLJYD-ICS-68_20150609040506.old'
#raw_f = '/home/taoyx/program_develop/python_dev/a.html';

gzip_f2 = '/opt/log/raw/access.log_HLJYD-ICS-68_20150609040506.gz'
#gzip_f2 = '/home/taoyx/program_develop/python_dev/log_gz/cpm.access.log-20150225.gz'
#gzip_f2 = '/home/taoyx/program_develop/python_dev/a.html.1.gz'
compress_file(raw_f, gzip_f2)

#gunzip_f = '/home/taoyx/program_develop/python_dev/log_gz/cpm.access.log-20150225.old'
#gunzip_f = '/home/taoyx/program_develop/python_dev/a.html.1'
#uncompress_file(gzip_f2, gunzip_f)

请自己修改输入和输出文件路径, 确保相应的文件存在, 取消相应的注释进行测试.

参考文献:

[1].https://docs.python.org/2/library/gzip.html   官网

[2].http://blog.csdn.net/jhonguy/article/details/7867348