''' 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()