Created
July 27, 2012 05:11
-
-
Save SAPikachu/3186273 to your computer and use it in GitHub Desktop.
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
import sys | |
import os | |
import shutil | |
import zipfile | |
def run(): | |
encoding_name = sys.argv[1] | |
path = len(sys.argv) > 3 and sys.argv[3] or "" | |
z = zipfile.ZipFile(sys.argv[2]) | |
for info in z.infolist(): | |
name = info.filename.encode("cp437").decode(encoding_name) | |
member = info | |
targetpath = path | |
# build the destination pathname, replacing | |
# forward slashes to platform specific separators. | |
# Strip trailing path separator, unless it represents the root. | |
if (targetpath[-1:] in (os.path.sep, os.path.altsep) | |
and len(os.path.splitdrive(targetpath)[1]) > 1): | |
targetpath = targetpath[:-1] | |
# don't include leading "/" from file name if present | |
if name[0] == '/': | |
targetpath = os.path.join(targetpath, name[1:]) | |
else: | |
targetpath = os.path.join(targetpath, name) | |
targetpath = os.path.normpath(targetpath) | |
# Create all upper directories if necessary. | |
upperdirs = os.path.dirname(targetpath) | |
if upperdirs and not os.path.exists(upperdirs): | |
os.makedirs(upperdirs) | |
if member.filename[-1] == '/': | |
if not os.path.isdir(targetpath): | |
os.mkdir(targetpath) | |
continue | |
source = z.open(info) | |
target = open(targetpath, "wb") | |
shutil.copyfileobj(source, target) | |
source.close() | |
target.close() | |
z.close() | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment