python gzip+base64方式压缩

gzip方式下的两种压缩:

1. gzip+base64方式压缩,如果压缩对象本事比较小,压缩后因为base64缘故反而会变大,因此,只有长度很大,要利于传输数据才采用该方式;

2. gzip原始方式压缩。

#!/usr/bin/python3  
# encoding: utf-8  
""" 
@version: v1.0 
@author: W_H_J 
@license: Apache Licence  
@contact: [email protected] 
@software: PyCharm 
@file: gzipAndBase64.py
@time: 2019/7/9 10:09 
@describe: gzip方式压缩与解压
"""
import sys
import os
import base64
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
sys.path.append("..")
import gzip  # 导入python gzip模块,注意名字为全小写


def gzip_zip_base64(content):
    """
    gzip+base64压缩
        这种方式在压缩对象比较短/小的时候效果并不明显,因为base64之后反而会变大,但是压缩对象比较大时才有效果
    :param content:
    :return:
    """
    bytes_com = gzip.compress(str(content).encode("utf-8"))
    base64_data = base64.b64encode(bytes_com)
    back = str(base64_data.decode())
    return back, sys.getsizeof(back)
    # 对gzip文件的读写操作
    # with gzip.open('./log/zip-test.gz', 'wb') as write:
    #     write.write(str(back).encode("utf-8"))


def gzip_unzip_base64(content):
    """
    base64+gzip解压
    :param content:
    :return:
    """
    base64_data = base64.b64decode(content)
    bytes_decom = gzip.decompress(base64_data)
    str_unzip = bytes_decom.decode()
    return str_unzip, sys.getsizeof(str_unzip)


def gzip_zip(content):
    """
    gzip方式压缩数据
    :param content: 要压缩对象
    :return: [0, 1]: 0:压缩对象;1:压缩后大小
    """
    bytes_com = gzip.compress(str(content).encode("utf-8"))
    return bytes_com, sys.getsizeof(bytes_com)
    # print(bytes_com)
    # with gzip.open('./lognew/zip-base-test.gz', 'wb') as write:
    #     write.write(str(content).encode("utf-8"))


def gzip_unzip(content):
    """
    gzip unzip 方式解压数据
    :param content: 要解压对象
    :return:
    """
    bytes_decom = gzip.decompress(content).decode()
    return bytes_decom, sys.getsizeof(bytes_decom)


if __name__ == '__main__':
    str_test = "python 实现gzip压缩并base编码"
    # sys.getsizeof()  # 返回该对象的在内存中所占字节大小
    
    # 原始方式压缩结果
    long_str = len(str_test)
    size_str = sys.getsizeof(str_test)
    gzip_str = gzip_zip(str_test)
    gunzip_str = gzip_unzip(gzip_str[0])
    print("原始字符串:{0}; \n压缩后字符串:{1};\n解压后对象:{2};".format(str_test, gzip_str[0], gunzip_str[0]))
    print("原始字符串长度:{0};\n原始字符串占内存字节大小:{1};\n压缩后占内存字节大小:{2};\n解压后占内存大小:{3}".format(long_str, size_str, gzip_str[1], gunzip_str[1]))

结果:

原始字符串:python 实现gzip压缩并base编码; 
压缩后字符串:b'\x1f\x8b\x08\x00\xe6\xbe6]\x02\xff\x01$\x00\xdb\xffpython \xe5\xae\x9e\xe7\x8e\xb0gzip\xe5\x8e\x8b\xe7\xbc\xa9\xe5\xb9\xb6base\xe7\xbc\x96\xe7\xa0\x815\x83@\xc2$\x00\x00\x00';
解压后对象:python 实现gzip压缩并base编码;
原始字符串长度:22;
原始字符串占内存字节大小:118;
压缩后占内存字节大小:92;
解压后占内存大小:118

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章