python自带了zipfile,貌似支持ZIP64,看帮助文档里好像有个选项
今天我的工作只是备份,所以只是创建zip档,其他就不关心了 ……
#!/usr/bin/env python
#coding=gbk
# python[AT]Live.it
import os
import sys
import getopt
import string
import zipfile
# print Help message
def Help():
print "Usage : python %s -t D://dir -z test.zip" %sys.argv[0]
sys.exit(0)
# get options
try:
opts , args = getopt.getopt(sys.argv[1:], "ht:z:")
except getopt.GetoptError:
print "/tBad arguments !"
Help()
# enum options
if 0 == len(opts):
Help()
for o,v in opts:
if ‘-h‘ == o.lower():
Help()
if ‘-t‘ == o.lower():
target = v
if ‘-z‘ == o.lower():
zipname = v
# zip directory
def zipDirectory(dir):
dir = dir.strip()
for (root,dirs,files) in os.walk(dir):
for filename in files:
print "Zip : %s" %(root+os.sep+filename)
z.write(root+os.sep+filename)
z.close()
# zip single file
def zipSingleFile(singleFile):
print "Zip : %s" %singleFile
singleFile = singleFile.strip()
z.write(singleFile )
z.close()
# run it
if os.path.isdir(target):
z = zipfile.ZipFile(zipname,‘w‘)
zipDirectory(target)
if os.path.isfile(target):
z = zipfile.ZipFile(zipname,‘w‘)
zipSingleFile(target)
run it
D:/>python zip.py -t D:/WIR -z E:/wir.zip
Zip : D:/WIR/Clip.py
Zip : D:/WIR/getClip.pl
Zip : D:/WIR/getClip.py
Zip : D:/WIR/openfiles.bmp
Zip : D:/WIR/Thumbs.db
Zip : D:/WIR/01/logosessions.png
Zip : D:/WIR/01/netsession.png
Zip : D:/WIR/01/netstat.png
Zip : D:/WIR/01/psloggedon.png
Zip : D:/WIR/01/tcpvcon.png
Zip : D:/WIR/01/wir
D:/>ls E:/wir.zip
E:/wir.zip