I use python's zipfile module to extract a .zip archive (Let's take this file at http://img.dafont.com/dl/?f=akvaleir for example.)
f = zipfile.ZipFile('akvaleir.zip', 'r')
for fileinfo in f.infolist():
print fileinfo.filename
f.extract(fileinfo, '.')
Its output:
Akval�ir_Normal_v2007.ttf
Akval�ir, La police - The Font - Fr - En.pdf
Both files are unaccessable after extraction because there are invalid encoded characters in their filenames. The problem is zipfile module doesn't have an option to specify output filenames.
However, "unzip akvaleir.zip" escapes the filename well:
root@host:~# unzip akvaleir.zip
Archive: akvaleir.zip
inflating: AkvalВir_Normal_v2007.ttf
inflating: AkvalВir, La police - The Font - Fr - En.pdf
I tried capturing output of "unzip -l akvaleir.zip" in my python program and these two filenames are:
Akval\xd0\x92ir_Normal_v2007.ttf
Akval\xd0\x92ir, La police - The Font - Fr - En.pdf
How can I get the correct filename like what unzip command does without capturing output of "unzip -l akvaleir.zip"?
It took some time but I think I found the answer.
I assumed the word was supposed to be Akvaléir. I found a page description about that, in French. When I used your code snippet I had a string like
That didn't work at UTF8, Latin-1, CP-1251 or CP-1252 encodings. I then found that CP863 was a possible Canadian encoding, so perhaps this was from French Canada.
However, I then read the Zip file format specification which says
Testing that out gives me the same answer as the Canadian code page
I don't have a Unicode encoded zip file and I'm not going to create one to find out, so I'll just assume that all zip files have the cp437 encoding.
On my Mac that gives
which tab-completes to
and shows up with a nice 'é' in my file browser.
I ran into a similar issue while running my application using Docker. Adding this lines to the Dockerfile, fixed everything for me:
So, I guess if you're not using Docker, give it a try and make sure locales are properly generated and set.
Instead of the
extract
method, use theopen
method and save the resulting pseudofile to disk under whatever name you wish, for example withshutil.copyfileobj
.