【www.bbyears.com--python】
代码如下
#!/usr/bin/python
#-*-coding:cp936 -*-
import os
import zipfile
def unzip(infile,targetDir):
if not os.path.exists(infile):
return False
if not os.path.isdir(targetDir):
os.makedirs(targetDir)
try:
zipObj = zipfile.ZipFile(infile)
files = zipObj.namelist()
for name in files:
if name[-1] == "/": # is a directory
if not os.path.isdir(os.path.join(targetDir,name)): # not exists
os.makedirs(os.path.join(targetDir,name))
else:
f = open(os.path.join(targetDir,name),"wb")
f.write(zipObj.read(name))
f.close()
zipObj.close()
except Exception as e:
print e
return False
if __name__ == "__main__":
currDir = os.path.dirname(os.path.abspath(__file__))
unzip("test.zip",currDir)