不灭的焱

加密类型:SHA/AES/RSA下载Go
复合类型:切片(slice)、映射(map)、指针(pointer)、函数(function)、通道(channel)、接口(interface)、数组(array)、结构体(struct) Go类型+零值nil
引用类型:切片(slice)、映射(map)、指针(pointer)、函数(function)、通道(channel) Go引用

作者:AlbertWen  添加时间:2021-01-20 22:42:20  修改时间:2025-11-18 07:15:57  分类:22.Python编程  编辑

分为两种情况:

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


def gzip_compress(raw_data):
    return gzip.compress(raw_data.encode())


def gzip_uncompress(compress_data):
    return gzip.decompress(compress_data).decode()


def gzip_compress_file(filename_in, filename_out):
    f_in = open(filename_in, 'rb')
    f_out = gzip.open(filename_out, 'wb')
    f_out.writelines(f_in)
    f_out.close()
    f_in.close()


def gzip_uncompress_file(filename_in, filename_out):
    f_in = gzip.open(filename_in, 'rb')
    f_out = open(filename_out, 'wb')
    file_content = f_in.read()
    f_out.write(file_content)
    f_out.close()
    f_in.close()

 

 

参考:

python3 gzip 压缩/解压

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