• python开发_gzip_压缩|解压缩gz文件_完整版_博主推荐


    '''
        gzip -- 支持gzip文件
        
        源文件:Lib/gzip.py
    
        这个模块提供了一些简单的接口来对文件进行压缩和解压缩,类似于GNU项目的gzip和gunzip。
    
        数据的压缩源于zlib模块的支持。
    
        在gzip模块提供了GzipFile类,在该类中提供了像open(),compress()和depress()等一些方便的方法
        GzipFile类在读写gzip格式的文件的时候,自动的压缩和解压缩数据类似于操作普通的文件对象。
    
        在gzip模块定义了一些方法:
    
        gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
            打开一个gzip已经压缩好的gzip格式的文件,并返回一个文件对象:file object.
            参数filename可以是真是的文件名(a str or bytes对象),或着是已经存在的读写文件对象。
            参数mode在操作二进制的时候使用:'r','rb','a','ab','wb'
                     操作text的时候使用:'rt,'at','wt'
                     默认是:'rb'
            参数compresslevel是0-9的数值。
    
        class gzip.GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
            
    '''

    运行效果:

    ====================================================

    代码部分:

    ====================================================

      1 #python gzip module
      2 
      3 #Author : Hongten
      4 #MailTo : hongtenzone@foxmail.com
      5 #QQ     : 648719819
      6 #Blog   : http://www.cnblogs.com/hongten
      7 #Create : 2013-08-19
      8 #Version: 1.0
      9 
     10 import os
     11 import gzip
     12 '''
     13     gzip -- 支持gzip文件
     14     
     15     源文件:Lib/gzip.py
     16 
     17     这个模块提供了一些简单的接口来对文件进行压缩和解压缩,类似于GNU项目的gzip和gunzip。
     18 
     19     数据的压缩源于zlib模块的支持。
     20 
     21     在gzip模块提供了GzipFile类,在该类中提供了像open(),compress()和depress()等一些方便的方法
     22     GzipFile类在读写gzip格式的文件的时候,自动的压缩和解压缩数据类似于操作普通的文件对象。
     23 
     24     在gzip模块定义了一些方法:
     25 
     26     gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
     27         打开一个gzip已经压缩好的gzip格式的文件,并返回一个文件对象:file object.
     28         参数filename可以是真是的文件名(a str or bytes对象),或着是已经存在的读写文件对象。
     29         参数mode在操作二进制的时候使用:'r','rb','a','ab','wb'
     30                  操作text的时候使用:'rt,'at','wt'
     31                  默认是:'rb'
     32         参数compresslevel是0-9的数值。
     33 
     34     class gzip.GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
     35         
     36 '''
     37 #运行此文件的时候,你只需要创建txt文件的存放位置即可
     38 #gz文件系统可以自动创建
     39 
     40 #global var
     41 #是否显示日志信息
     42 SHOW_LOG = True
     43 #gz文件存放位置
     44 GZ_FILE_PATH = ''
     45 #txt文件存放位置
     46 TXT_FILE_PATH = ''
     47 
     48 def read_gz_file(path):
     49     '''read the existing gzip-format file,and return the content of this file'''
     50     if os.path.exists(path):
     51         #the function open(filename, mode = 'rb'),so the mode argument is default is 'rb'
     52         if SHOW_LOG:
     53             print('打开文件:[{}]'.format(path))
     54         with gzip.open(path, 'rb') as pf:
     55             return pf.read()
     56     else:
     57         print('the path [{}] is not exist!'.format(path))
     58 
     59 def write_gz_file(path, content):
     60     '''write the byte-format content into the gzip-format file
     61     so,with this way,we can creat the file if the path doesn't exist.will
     62     we can write the content into the file if the file existing'''
     63     if SHOW_LOG:
     64         print('写入文件:[{}] 内容:[{}]'.format(path, content))
     65     with gzip.open(path, 'wb') as f:
     66         f.write(content)
     67         
     68 def read_txt_write_gz(tpath, gzpath):
     69     '''read the txt-format file with 'rb' and write this file content
     70     to the gzip-format file'''
     71     if os.path.exists(tpath):
     72         if os.path.exists(gzpath):
     73             if SHOW_LOG:
     74                 print('打开文件:[{}]'.format(tpath))
     75             with open(tpath, 'rb') as t:
     76                 if SHOW_LOG:
     77                     print('打开文件:[{}]'.format(gzpath))
     78                 with gzip.open(gzpath, 'wb') as g:
     79                     if SHOW_LOG:
     80                         print('写入内容:[{}]'.format(t))
     81                     g.writelines(t)
     82                     if SHOW_LOG:
     83                         print('写入内容完成...')
     84         else:
     85             print('the path [{}] is not exist!'.format(gzpath))
     86     else:
     87         print('the path [{}] is not exist!'.format(tpath))
     88 
     89 def init():
     90     global SHOW_LOG
     91     SHOW_LOG = True
     92     #gz文件存放位置
     93     global GZ_FILE_PATH
     94     GZ_FILE_PATH = 'c:\test\hongten.txt.gz'
     95     #txt文件存放位置
     96     global TXT_FILE_PATH
     97     TXT_FILE_PATH = 'c:\test\honngten_info.txt'
     98 
     99 def main():
    100     init()
    101     content = b'this is a byte message!'
    102     write_gz_file(GZ_FILE_PATH, content)
    103     con =  read_gz_file(GZ_FILE_PATH)
    104     print(con)
    105     print('#' * 50)
    106     content_str = 'this is a str message!'
    107     write_gz_file(GZ_FILE_PATH, bytes(content_str, 'utf-8'))
    108     con = read_gz_file(GZ_FILE_PATH)
    109     print(con)
    110     print('#' * 50)
    111     read_txt_write_gz(TXT_FILE_PATH, GZ_FILE_PATH)
    112     con = read_gz_file(GZ_FILE_PATH)
    113     print(con)
    114 
    115 if __name__ == '__main__':
    116     main()
  • 相关阅读:
    【Qt】无边框窗体中带有ActiveX组件时的一个BUG
    Qt:正确判断文件、文件夹是否存在的方法
    自定义Data Service Providers
    Facebook的ATOM Editor的底层Electron
    ASP.NET Web API中使用OData
    Oracle 使用
    ODP.NET 之访问 Oracle 数据库
    Oracle安装及使用入门
    架构设计
    CQRS模式实现
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_python_gzip.html
Copyright © 2020-2023  润新知