python gzip模块实现文件压缩的方法
2014-12-15 22:13
288 查看
pythongzip模块实现文件压缩的方法
使用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()
使用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()
相关文章推荐
- php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)
- Java实现多文件压缩打包的方法
- Django压缩静态文件的实现方法详析
- c#自带压缩类实现数据库表导出到CSV压缩文件的方法
- php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)
- Java实现自动压缩文件并加密的方法示例
- RequireJS对文件合并与压缩实现方法
- Android编程实现将压缩数据库文件拷贝到安装目录的方法
- Python实现多级目录压缩与解压文件的方法
- php实现压缩多个CSS与JS文件的方法
- C#实现Zip压缩目录中所有文件的方法
- C#使用iCSharpcode进行文件压缩实现方法
- java 压缩打包文件的实现方法
- php实现压缩多个CSS与JS文件的方法
- PHP压缩文件夹的具体实现方法
- java实现 zip压缩文件 三种方法
- C#实现文件压缩与解压的方法示例【ZIP格式】
- Java处理压缩文件或文件夹相关的一些方法的实现
- Python实现压缩与解压gzip大文件的方法