别人从Windows系统下发给我一个zip文件,我拿到Linux平台解压之后发现文件名全是乱码。
在网上找了很多方案,但是大部分都有点麻烦。
闲着没事,用Python写了一个zip解压脚本,能够解决乱码问题:
源码在这里:
#-*- coding:utf8 -*-
'''
gbkzip - unzip Windows zip file and convert gbk to utf8
author: YUCOAT(yucoat^yucoat.com)
date: 2012-9-28
homepage: www.yucoat.com
If you find any bug please contact me, thank you!
'''
import os
import sys
import zipfile
if __name__ == '__main__':
if len(sys.argv) != 2:
print 'Usage: gbkzip < zipfile.zip >'
exit(1)
try:
zip_obj = zipfile.ZipFile(sys.argv[1], 'r')
except IOError as e:
print e.strerror
exit(1)
for name in zip_obj.namelist():
name_utf8 = name.decode('gb2312')
path = os.path.dirname(name_utf8)
if (not os.path.exists(path)) and path:
os.makedirs(path)
#Start Exacting
filedata = zip_obj.read(name)
if not os.path.exists(name_utf8):
tmp = open(name_utf8, 'w')
tmp.write(filedata)
tmp.close()
print 'Exacting %s ... done!'% (name_utf8)
zip_obj.close()
该程序的使用说明已经最新版本我都会放在github的这个页面
如果有什么问题,可以给我发邮件:thlgood ^ yucoat.com

