def unzip_fromfile(zip_path, dest_path):
'''method to unzip a zipfile to a destination path'''
import shutil
import zipfile
zip_path = xbmc.translatePath(zip_path).decode("utf-8")
dest_path = xbmc.translatePath(dest_path).decode("utf-8")
log_msg("START UNZIP of file %s to path %s " % (zip_path, dest_path))
zip_file = zipfile.ZipFile(zip_path, 'r')
for fileinfo in zip_file.infolist():
filename = fileinfo.filename
if not isinstance(filename, unicode):
filename = filename.decode("utf-8")
log_msg("unzipping: " + filename)
splitter = None
if "\\" in filename:
xbmcvfs.mkdirs(os.path.join(dest_path, filename.rsplit("\\", 1)[0]))
splitter = "\\"
elif "/" in filename:
xbmcvfs.mkdirs(os.path.join(dest_path, filename.rsplit("/", 1)[0]))
splitter = "/"
filename = os.path.join(dest_path, filename)
if not (splitter and filename.endswith(splitter)):
try:
# newer python uses unicode
outputfile = open(filename, "wb")
except Exception:
# older python uses utf-8
outputfile = open(filename.encode("utf-8"), "wb")
# use shutil to support non-ascii formatted files in the zip
shutil.copyfileobj(zip_file.open(fileinfo.filename), outputfile)
outputfile.close()
zip_file.close()
log_msg("UNZIP DONE of file %s to path %s " % (zip_path, dest_path))
utils.py 文件源码
python
阅读 21
收藏 0
点赞 0
评论 0
评论列表
文章目录