Skip to content

Instantly share code, notes, and snippets.

@jostyee
Created March 9, 2018 04:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jostyee/b4eacef8db01132fb86b6de4b959f077 to your computer and use it in GitHub Desktop.
Save jostyee/b4eacef8db01132fb86b6de4b959f077 to your computer and use it in GitHub Desktop.
gbkzip - unzip Windows zip file and convert gbk to utf8 using Python 3
#!/usr/bin/env python
#-*- coding:utf8 -*-
'''
YUCOAT的原脚本只适用于Python 2,改了下Python 3的版本
'''
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.encode('cp437').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(str(filedata, 'utf-8'))
tmp.close()
print('Exacting %s ... done!'% (name_utf8))
zip_obj.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment