Created
May 5, 2016 01:34
-
-
Save fangdingjun/0332b86b2ae43ad7347d76652adee962 to your computer and use it in GitHub Desktop.
extract a zip file with non-utf8 encoded filenames under 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
#!python | |
import zipfile | |
import os | |
def unzip(filename, output_dir=".", filename_encode="gbk"): | |
fp = zipfile.ZipFile(filename, "r") | |
# get name list | |
nlist = fp.namelist() | |
for name in nlist: | |
# decode filename | |
n1 = name.decode(filename_encode) | |
# ignore directory | |
if n1.endswith("/"): | |
continue | |
# full filename | |
n2 = os.path.join(output_dir, n1) | |
# directory name | |
d = os.path.dirname(n2) | |
if not os.path.isdir(d): | |
os.makedirs(d, 0755) | |
# extract content | |
with open(n2, "wb") as fp1: | |
fp1.write(fp.read(name)) | |
fp.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment