Skip to content

Instantly share code, notes, and snippets.

@waynema02
Created January 5, 2018 03:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynema02/493233c36421f844b559d094fc13660c to your computer and use it in GitHub Desktop.
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
#!/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