python压缩文件_python怎样压缩和解压缩ZIP文件

展开全部

一、读取e69da5e887aa3231313335323631343130323136353331333361323030zip文件

首先,通过zipfile模块打开指定zip文件,如:

zpfd = zipfile.ZipFile(path, mode='r')

对于zipfile,其标志与open所用的打开文件标志有所不同,不能识别 'rb'。

然后,读取zip文件中的内容,zipfile对象提供一个read(name)的方法,name为zip文件中的一个文件入口,执行完成之后,将返回读出的内容,如:

for filename in zpfd.namelist():

tmpcont = zpfd.read(filename)

print 'len(tmpcont)', 'tmpcont'

需要注意的是,读取zip文件时,只能读取内容

二、写入zip文件

首先,需要zipfile模块写打开或创建zip文件,如:

zpfd = zipfile.ZipFile(path, mode='w')

写打开是标志可以为'w'或'a'('a'表示写入一个zip文件), 或者传入第三个参数cmopression压缩标志

compression=zipfile.ZIP_DEFLATED 需要导入zlib模块

compression=zipfile.ZIP_STORED则表示只对文件进行打包,并不压缩

写入有两种方式,一种是直接写入一个已经存在的文件,可使用zipfile对象中write(filename, arcname, compress_type)第一个参数为文件名,第二个参数指写入zip文件中的文件名,默认与filename一致,第三个参数压缩标志可以覆盖打开zipfile时的使用参数;另一种是写入一个字符串,可使用zipfile对象中的writestr(zinfo_or_arcname, bytes),第一个参数是zipinfo对象或写到zip文件中的压缩名,第二个参数是待写入的字符串

最后,对于打开的zipfile对象需要进行关闭,从而使得写入内容真正写入磁盘,即:

zpfd.close()

以下是一个实现简单压缩与解压缩的实例import os,sys

import zipfile

def zip_dir(dirname, zipfilename):

filelist = []

#Check input ...

fulldirname = os.path.abspath(dirname)

fullzipfilename = os.path.abspath(zipfilename)

print "Start to zip %s to %s ..." % (fulldirname, fullzipfilename)

if not os.path.exists(fulldirname):

print "Dir/File %s is not exist, Press any key to quit..." % fulldirname

inputStr = raw_input()

return

if os.path.isdir(fullzipfilename):

tmpbasename = os.path.basename(dirname)

fullzipfilename = os.path.normpath(os.path.join(fullzipfilename, tmpbasename))

if os.path.exists(fullzipfilename):

print "%s has already exist, are you sure to modify it ? [Y/N]" % fullzipfilename

while 1:

inputStr = raw_input()

if inputStr == "N" or inputStr == "n" :

return

else:

if inputStr == "Y" or inputStr == "y" :

print "Continue to zip files..."

break

#Get file(s) to zip ...

if os.path.isfile(dirname):

filelist.append(dirname)

dirname = os.path.dirname(dirname)

else:

#get all file in directory

for root, dirlist, files in os.walk(dirname):

for filename in files:

filelist.append(os.path.join(root,filename))

#Start to zip file ...

destZip = zipfile.ZipFile(fullzipfilename, "w")

for eachfile in filelist:

destfile = eachfile[len(dirname):]

print "Zip file %s..." % destfile

destZip.write(eachfile, destfile)

destZip.close()

print "Zip folder succeed!"

def unzip_dir(zipfilename, unzipdirname):

fullzipfilename = os.path.abspath(zipfilename)

fullunzipdirname = os.path.abspath(unzipdirname)

print "Start to unzip file %s to folder %s ..." % (zipfilename, unzipdirname)

#Check input ...

if not os.path.exists(fullzipfilename):

print "Dir/File %s is not exist, Press any key to quit..." % fullzipfilename

inputStr = raw_input()

return

if not os.path.exists(fullunzipdirname):

os.mkdir(fullunzipdirname)

else:

if os.path.isfile(fullunzipdirname):

print "File %s is exist, are you sure to delet it first ? [Y/N]" % fullunzipdirname

while 1:

inputStr = raw_input()

if inputStr == "N" or inputStr == "n":

return

else:

if inputStr == "Y" or inuptStr == "y":

os.remove(fullunzipdirname)

print "Continue to unzip files ..."

break

#Start extract files ...

srcZip = zipfile.ZipFile(fullzipfilename, "r")

for eachfile in srcZip.namelist():

print "Unzip file %s ..." % eachfile

eachfilename = os.path.normpath(os.path.join(fullunzipdirname, eachfile))

eachdirname = os.path.dirname(eachfilename)

if not os.path.exists(eachdirname):

os.makedirs(eachdirname)

fd=open(eachfilename, "wb")

fd.write(srcZip.read(eachfile))

fd.close()

srcZip.close()

print "Unzip file succeed!"

def print_help(toolname):

print """

This program can zip given folder to destination file, or unzip given zipped file to destination folder.

Usage: %s [option] [arg]...

-h: print this help message and exit (also --help)

-u unzip: unzip given zipped file to destination folder,

usage: %s -u/-unzip zipfilename, unzipdirname

-z zip: zip given folder to destination file

usage: %s -z/zip dirname, zipfilename

"""  % (toolname, toolname, toolname)

if __name__ == '__main__':

if len(sys.argv) 

print_help(sys.argv[0])

sys.exit()

if sys.argv[1].startswith('--'):

option = sys.argv[1][2:]

if option == 'version':

print 'Get no version information'

else:

if option != 'help':

print "Unknown option"

print_help(sys.argv[0])

sys.exit()

if sys.argv[1].startswith('-'):

if len(sys.argv) 

print_help(sys.argv[0])

sys.exit()

option = sys.argv[1][1:]

if option == 'u' or option == 'unzip':

zipfilepath = sys.argv[2]

unzipdirpath = sys.argv[3]

unzip_dir(zipfilepath, unzipdirpath)

else:

if option == 'z' or option == 'zip':

dirpath = sys.argv[2]

zipfilepath = sys.argv[3]

zip_dir(dirpath, zipfilepath)

else:

print_help(sys.argv[0])

sys.exit()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值