Created
January 5, 2018 03:46
-
-
Save waynema02/493233c36421f844b559d094fc13660c to your computer and use it in GitHub Desktop.
Unzip the zip file encoded by gbk. ref: https://superuser.com/a/1202386
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# unzip-gbk.py | |
import os | |
import sys | |
import zipfile | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--encoding", help="encoding for filename, default gbk") | |
parser.add_argument("-l", help="list filenames in zipfile, do not unzip", action="store_true") | |
parser.add_argument("file", help="process file.zip") | |
args = parser.parse_args() | |
print "Processing File " + args.file | |
file=zipfile.ZipFile(args.file,"r"); | |
if args.encoding: | |
print "Encoding " + args.encoding | |
for name in file.namelist(): | |
if args.encoding: | |
utf8name=name.decode(args.encoding) | |
else: | |
utf8name=name.decode('gbk') | |
pathname = os.path.dirname(utf8name) | |
if args.l: | |
print "Filename " + utf8name | |
else: | |
print "Extracting " + utf8name | |
if not os.path.exists(pathname) and pathname!= "": | |
os.makedirs(pathname) | |
data = file.read(name) | |
if not os.path.exists(utf8name): | |
fo = open(utf8name, "w") | |
fo.write(data) | |
fo.close | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment