设为首页 收藏本站
查看: 988|回复: 0

[经验分享] python开发_gzip_压缩|解压缩gz文件_完整版_博主推荐

[复制链接]

尚未签到

发表于 2015-4-27 10:35:34 | 显示全部楼层 |阅读模式
'''
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)
'''
  运行效果:
DSC0000.png
DSC0001.png
  ====================================================
  代码部分:
  ====================================================



  1 #python gzip module
  2
  3 #Author : Hongten
  4 #MailTo : hongtenzone@foxmail.com
  5 #QQ     : 648719819
  6 #Blog   : http://www.iyunv.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()
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61145-1-1.html 上篇帖子: Python的Base64编码图片(转载) 下篇帖子: 转:Windows下配置python环境变量
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表