BadZipfile "file is not a zip file"

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jan 9 03:16:17 EST 2009


On Thu, 08 Jan 2009 16:47:39 -0800, webcomm wrote:

> The error...
...
> BadZipfile: File is not a zip file
> 
> When I look at data.zip in Windows, it appears to be a valid zip file. 
> I am able to uncompress it in Windows XP, and can also uncompress it
> with 7-Zip.  It looks like zipfile is not able to read a "table of
> contents" in the zip file.  That's not a concept I'm familiar with.

No, ZipFile can read table of contents:

    Help on method printdir in module zipfile:

    printdir(self) unbound zipfile.ZipFile method
        Print a table of contents for the zip file.



In my experience, zip files originating from Windows sometimes have 
garbage at the end of the file. WinZip just ignores the garbage, but 
other tools sometimes don't -- if I recall correctly, Linux unzip 
successfully unzips the file but then complains that the file was 
corrupt. It's possible that you're running into a similar problem.


> data.zip is created in this script...
> 
>     decoded = base64.b64decode(datum)
>     f = open('data.zip', 'wb')
>     f.write(decoded)
>     f.close()
>     file = zipfile.ZipFile('data.zip', "r")
> 
> datum is a base64 encoded zip file.  Again, I am able to open data.zip
> as if it's a valid zip file.  Maybe there is something wrong with the
> approach I've taken to writing the data to data.zip?  I'm not sure if it
> matters, but the zipped data is Unicode.


The full signature of ZipFile is:

ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True)

Try passing compression=zipfile.ZIP_DEFLATED and/or allowZip64=False and 
see if that makes any difference.

The zip format does support alternative compression methods, it's 
possible that this particular file uses a different sort of compression 
which Python doesn't deal with.


> What would cause a zip file to not have a table of contents?

What makes you think it doesn't have one?


-- 
Steven



More information about the Python-list mailing list