Python压缩与解压缩文件
Python能够直接处理zip文件中的数据,例如需要将对应目录或多文件打包或压缩成zip格式,或者需要查看一个zip格式的归档文件中部分或所有的文件同时避免将这些文件展开到磁盘上,自1.6版本起,python中zipfile模块以实现相应操作。不过zipfile模块目前还不能处理分卷zip文件和带有注释的zip文件。
目前仅整理对zip文件的读取与写入的两种基本操作。
一、读取zip文件
首先,通过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 = []
-
- 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
-
-
- if os.path.isfile(dirname):
- filelist.append(dirname)
- dirname = os.path.dirname(dirname)
- else:
-
- for root, dirlist, files in os.walk(dirname):
- for filename in files:
- filelist.append(os.path.join(root,filename))
-
-
- 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)
-
- 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
-
-
- 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
-
-
-
-
-
-
-
- % (toolname, toolname, toolname)
-
- if __name__ == '__main__':
- if len(sys.argv) < 2:
- 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) < 4 :
- 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()