1. 程式人生 > >利用python中的gzip模組壓縮和解壓資料流和檔案

利用python中的gzip模組壓縮和解壓資料流和檔案

直接給出原始碼實現, 分為兩種情況:

1.網路連線中的資料流的壓縮和解壓,或是開啟的檔案讀取一部分

2.開啟檔案壓縮或是解壓

#!/usr/bin/env python
#encoding: utf-8
#filename: gzip_demo.py
#author: [email protected]
#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)

請自己修改輸入和輸出檔案路徑, 確保相應的檔案存在, 取消相應的註釋進行測試.

參考文獻: