时常遇到windows上发过来的压缩包,在Mac上解压出来,文件名就成乱码了,找不到很好的命令来在解压时自动将文件名进行转码,没有银弹,所以只能写代码实现。一段python脚本就能够很好的处理,代码源自网上,然后自己改写了一把:
#coding:utf8
import os
import sys
import zipfile
with zipfile.ZipFile(sys.argv[1], "r") as zf:
for name in zf.namelist():
utf8name = name.decode('gbk').encode('utf8')
print "Extracting " + utf8name
pathname = os.path.dirname(utf8name)
#创建目录
if not os.path.exists(pathname) and pathname != "":
os.makedirs(pathname)
#不覆盖
if not os.path.exists(utf8name):
with open(utf8name, "w") as fo:
fo.write(zf.read(name))