Created
June 17, 2015 06:20
-
-
Save janpipek/3ac2fabf7f47e39fa3d1 to your computer and use it in GitHub Desktop.
Unzip ZIP packages with non-ASCII encoding on Linux
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 python | |
import os | |
import zipfile | |
import sys | |
def unzip_with_encoding(archive_path, output_path, encoding): | |
zz = zipfile.ZipFile(archive_path) | |
for fp in zz.filelist: | |
bytes = zz.read(fp.filename) | |
name = fp.filename.decode(encoding) | |
new_name = os.path.join(output_path, name) | |
if name[-1] == "/": | |
if not os.path.isdir(new_name): | |
os.makedirs(new_name) | |
else: | |
with open(new_name, "wb") as f: | |
f.write(bytes) | |
if __name__ == "__main__": | |
if len(sys.argv) > 3: | |
outpath = sys.argv[2] | |
else: | |
outpath = "." | |
if len(sys.argv) > 4: | |
encoding = sys.argv[3] | |
else: | |
encoding = "cp852" # Typical for Czech file-systems | |
zippath = sys.argv[1] | |
unzip_with_encoding(zippath, outpath, encoding) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment