Skip to content

Instantly share code, notes, and snippets.

@hideaki-t
Created May 18, 2014 07:34
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hideaki-t/c42a16189dd5f88a955d to your computer and use it in GitHub Desktop.
Save hideaki-t/c42a16189dd5f88a955d to your computer and use it in GitHub Desktop.
unzipping a zip file with non-utf8 encoding by Python3
import zipfile
import sys
from pathlib import Path
def unzip(f, encoding, v):
with zipfile.ZipFile(f) as z:
for i in z.namelist():
n = Path(i.encode('cp437').decode(encoding))
if v:
print(n)
if i[-1] == '/':
if not n.exists():
n.mkdir()
else:
with n.open('wb') as w:
w.write(z.read(i))
if __name__ == '__main__':
for i in sys.argv[1:]:
unzip(i, 'cp932', 1)
@opilar
Copy link

opilar commented Aug 11, 2014

Thanks. This saves a lot of time.

@wugh
Copy link

wugh commented Jun 8, 2018

Thanks

@Fighter19
Copy link

Thanks

@easz
Copy link

easz commented Jun 10, 2019

it works well.

but why cp437?

and why not ZipInfo.is_dir to test if it is directory?

@easz
Copy link

easz commented Jun 10, 2019

cool!

@shwangdev
Copy link

Not robust enough, but still worked like a charm. Inspired me to enhance a new version of this.

@vermouthmjl
Copy link

Incredible! I struggled an hour for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment