Created
February 6, 2014 16:05
-
-
Save maliubiao/8847159 to your computer and use it in GitHub Desktop.
ezip.py, handle encoded filename in a zip file.
This file contains 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
import zipfile | |
import sys | |
import os.path | |
import errno | |
def files_in_zip(this_zip, encoding): | |
return [x.decode(encoding) for x in zipfile.ZipFile(this_zip).namelist()] | |
def extract_all_encoding(this_zip, path_prefix, encoding): | |
z = zipfile.ZipFile(this_zip) | |
system_encoding = sys.getfilesystemencoding() | |
for i in z.infolist(): | |
if i.filename.endswith("/"): | |
continue | |
try: | |
unicode_name = i.filename.decode(encoding) | |
except UnicodeError: | |
print "Wrong Encoding? Failed" | |
break | |
fn = unicode_name.encode(system_encoding) | |
origin = z.open(i.filename) | |
final = os.path.join(path_prefix, fn) | |
try: | |
os.makedirs(os.path.dirname(final)) | |
except OSError as e: | |
if e.errno != errno.EEXIST: | |
print "failed to create directory: ", os.path.dirname(final) | |
break | |
dst = open(final, "w") | |
dst.write(origin.read()) | |
dst.close() | |
origin.close() | |
def print_usage(): | |
print """usage: python ezip.py | |
-e encoding to decode filename, zipfile. list all files in a zip. | |
-a encoding to decode filename, zipfile, dstpath, extract all files in archive to dstpath. | |
""" | |
if __name__ == "__main__": | |
try: | |
arg1 = sys.argv[1] | |
except: | |
print_usage() | |
exit() | |
if arg1 == "-e": | |
encoding = sys.argv[2] | |
path = sys.argv[3] | |
for i in files_in_zip(path, encoding): | |
print i | |
elif arg1 == "-a": | |
encoding = sys.argv[2] | |
path = sys.argv[3] | |
dst = sys.argv[4] | |
extract_all_encoding(path, dst, encoding) | |
else: | |
print_usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment