Created
January 27, 2016 03:54
-
-
Save mitsuse/123e7f6b846fdef01c73 to your computer and use it in GitHub Desktop.
Unzip files encoded with cp932.
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
#!/usr/bin/env python3 | |
# coding: utf-8 | |
def main(context): | |
from zipfile import ZipFile | |
with ZipFile(context.archive) as f: | |
if context.password is not None: | |
f.setpassword(context.password.encode()) | |
for info in f.infolist(): | |
extract(f, info.filename, context.dest) | |
def extract(zip_file, name, dest): | |
import os | |
from os import path | |
p = path.join( | |
dest, | |
name.encode('cp437').decode('cp932') | |
) | |
os.makedirs(path.dirname(p), exist_ok=True) | |
if is_path_of_dir(p): | |
return | |
with open(p, 'wb') as f: | |
f.write(zip_file.read(name)) | |
def is_path_of_dir(p): | |
from os import path | |
return p.endswith('/') | |
def parse_args(args): | |
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument( | |
'--archive', | |
'-a', | |
type=str, | |
required=True, | |
help='the path of a ZIP file', | |
) | |
parser.add_argument( | |
'--password', | |
'-p', | |
type=str, | |
required=False, | |
help='the password of files to be extracted', | |
) | |
parser.add_argument( | |
'--dest', | |
'-d', | |
default='.', | |
type=str, | |
required=False, | |
help='the destination path of extracted files', | |
) | |
return parser.parse_args(args) | |
if __name__ == '__main__': | |
import sys | |
main(parse_args(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment