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 importos11 importgzip12 '''
13 gzip -- 支持gzip文件14
15 源文件:Lib/gzip.py16
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 =True43 #gz文件存放位置
44 GZ_FILE_PATH = ''
45 #txt文件存放位置
46 TXT_FILE_PATH = ''
47
48 defread_gz_file(path):49 '''read the existing gzip-format file,and return the content of this file'''
50 ifos.path.exists(path):51 #the function open(filename, mode = 'rb'),so the mode argument is default is 'rb'
52 ifSHOW_LOG:53 print('打开文件:[{}]'.format(path))54 with gzip.open(path, 'rb') as pf:55 returnpf.read()56 else:57 print('the path [{}] is not exist!'.format(path))58
59 defwrite_gz_file(path, content):60 '''write the byte-format content into the gzip-format file61 so,with this way,we can creat the file if the path doesn't exist.will62 we can write the content into the file if the file existing'''
63 ifSHOW_LOG:64 print('写入文件:[{}] 内容:[{}]'.format(path, content))65 with gzip.open(path, 'wb') as f:66 f.write(content)67
68 defread_txt_write_gz(tpath, gzpath):69 '''read the txt-format file with 'rb' and write this file content70 to the gzip-format file'''
71 ifos.path.exists(tpath):72 ifos.path.exists(gzpath):73 ifSHOW_LOG:74 print('打开文件:[{}]'.format(tpath))75 with open(tpath, 'rb') as t:76 ifSHOW_LOG:77 print('打开文件:[{}]'.format(gzpath))78 with gzip.open(gzpath, 'wb') as g:79 ifSHOW_LOG:80 print('写入内容:[{}]'.format(t))81 g.writelines(t)82 ifSHOW_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 definit():90 globalSHOW_LOG91 SHOW_LOG =True92 #gz文件存放位置
93 globalGZ_FILE_PATH94 GZ_FILE_PATH = 'c:\\test\\hongten.txt.gz'
95 #txt文件存放位置
96 globalTXT_FILE_PATH97 TXT_FILE_PATH = 'c:\\test\\honngten_info.txt'
98
99 defmain():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()