Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't unzip archive built with zipfile (Python)

I'm having problems with an archive that I built using zipfile in Python. I'm iterating over all the files in a directory and writing them to an archive. When I attempt to extract them afterward I get an exception related to the path separator.

the_path= "C:\\path\\to\\folder"
zipped= cStringIO.StringIO()
zf = zipfile.ZipFile(zipped_cache, "w", zipfile.ZIP_DEFLATED)
for dirname, subdirs, files in os.walk(the_path) :
    for filename in files:
        zf.write(os.path.join(dirname, filename), os.path.join(dirname[1+len(the_path):], filename))
zf.extractall("C:\\destination\\path")
zf.close()
zipped_cache.close()

Here's the exception:

zipfile.BadZipfile: File name in directory "env\index" and header "env/index" differ.

Update: I replaced the string buffer cStringIO.StringIO() with a temporary file (tempfile.mkstemp("temp.zip")) and now it works. There's something that happens when the zipfile module writes to the buffer that corrupts the archive, not sure what the problem is though.

The issue was that I was reading/writing the information from/into files that were open in "r"/"w" mode instead of "rb"/"wb". This isn't an issue in Linux, but it gave me errors in Windows due to character encoding. Solved.

like image 834
Cat Avatar asked Dec 22 '22 13:12

Cat


1 Answers

You should consider adding an r before the string to indicate it is a raw string -- the backslashes in the path are being interpreted as escape characters.

The following code:

#!/bin/env python    
print(r"C:\destination\path")
print(r"C:\path\to\folder")
print("C:\destination\path")
print("C:\path\to\folder")

produces the following output:

C:\destination\path
C:\path\to\folder
C:\destination\path
C:\path o
         older

Note that the \t and \f are interpreted as tab and formfeed in the last line.

Interestingly, you could also change the backslashes to forward slashes (i.e. open("C:/path/to/folder"), which would work.

Or, escape the backslashes with ... backslashes (i.e. open("C:\\path\\to\\folder")).

IMO, the clearest and easiest solution is to simply add an r.


Edit: It looks like you need to go with the second solution, forward slashes. The zipfile library is kind of strict apparently -- and given that this is a window-only bug, it probably snuck through. (See Issue 6839).

like image 99
jedwards Avatar answered Jan 10 '23 18:01

jedwards