BadZipfile "file is not a zip file"

John Machin sjmachin at lexicon.net
Fri Jan 9 04:37:50 EST 2009


On Jan 9, 7:16 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> 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.

"compression" is irrelevant when reading. The compression method used
is stored on a per-file basis, not on a per-archive basis, and it
hasn't got anywhere near per-file details when that exception is
raised. "allowZip64" has not been used either.

>
> 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