python gzip模块实现文件压缩的方法
来源:互联网 发布:java实现文件下载 编辑:程序博客网 时间:2024/05/16 12:06
python gzip模块实现文件压缩的方法
使用gzip格式压缩文件,注意引入gzip包。
代码:
#!/bin/python
#
#site: www.jbxue.com
import string
import gzip
from optparse import OptionParser
def readCommandLine():
parser = OptionParser()
#read the options in
parser.add_option("-f","--Full_file_location",
dest="File_to_be_run",
default=r"c:\tn.txt",
help="This is the fully qualified path name to the file location")
parser.add_option("-m","--Mode",
dest="modeTn",
default="r",
help="The mode of zip unzip")
parser.add_option("-c","--Compression",
dest="compress",
default=9,
help="The level of compression")
options, args = parser.parse_args()
#print options
return options
def zipit(filename, mode,compress):
#Saves/Zipps a compressed file to disk
#
r_file = open(filename, 'r')
# this is the zipping bit
w_file = gzip.GzipFile(filename + '.gz', mode, compress)
w_file.write(r_file.read())
w_file.flush()
w_file.close()
r_file.close()
def un_zipit(filename,mode):
#Unzips a compressed file from disk
#
#this is the unzipping bit
r_file = gzip.GzipFile(filename, mode)
write_file = string.rstrip(filename, '.gz')
w_file = open(write_file, 'w')
w_file.write(r_file.read())
w_file.close()
r_file.close()
if __name__ == "__main__":
#first thing to do is read the options in
options = readCommandLine()
if options.modeTn == "r":
#unzippit mode
if options.File_to_be_run[-3:] != '.gz':
# check to see if it has the extension .gz
print "This " + options.File_to_be_run + " is not a .gz file"
else:
#This should now unzipit
un_zipit(options.File_to_be_run,options.modeTn)
elif options.modeTn== "wb":
#this should zipit
zipit(options.File_to_be_run,options.modeTn,options.compress)
else:
# basically the wrong option was passed
print "ABORT something went wrong"
sys.exit()
- python gzip模块实现文件压缩的方法
- linux服务器的Gzip文件压缩方法
- 过滤器实现文件的gzip压缩
- php gzip压缩输出的实现方法
- php gzip压缩输出的实现方法
- 飘逸的python - 简明gzip模块压缩教程
- 飘逸的python - 简明gzip模块压缩教程
- Python Gzip压缩与解压模块
- IIS启用GZip压缩JS/CSS文件的设置方法
- GZIP文件压缩的应用
- Nginx模块参考手册:Gzip压缩模块(Gzip)
- 关于JavaScript的gzip静态压缩方法
- JAVA字符串的GZIP压缩解压缩方法
- tomcat开启gzip网页压缩的方法
- JAVA字符串的GZIP压缩解压缩方法
- Nginx 开启Gzip压缩的方法
- JAVA字符串的GZIP压缩解压缩方法
- Java实现Gzip的压缩和解压
- JSTL不同版本和EL表达式的关联
- Android EditText/TextView使用SpannableString显示复合文本
- MFC常用类:定时器Timer
- Java反射 获取某个类的属性值、字段名、注解等常用代码
- 湛江中医治疗慢性鼻炎秘方
- python gzip模块实现文件压缩的方法
- Service中如何调用国际化配置文件?
- Java中a=a+b 与 a+=b区别
- ocp 1Z0-051 23-70题解析
- Python处理各种压缩文件(bzip2,gzip,zip)
- 使用Geomagic处理点云一般步骤
- 用java代码实现百度搜索返回结果条数
- mongodb
- 抽奖小程序