BadZipfile "file is not a zip file"

webcomm ryandw at gmail.com
Thu Jan 8 19:47:39 EST 2009


The error...

>>> file = zipfile.ZipFile('data.zip', "r")
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    file = zipfile.ZipFile('data.zip', "r")
  File "C:\Python25\lib\zipfile.py", line 346, in __init__
    self._GetContents()
  File "C:\Python25\lib\zipfile.py", line 366, in _GetContents
    self._RealGetContents()
  File "C:\Python25\lib\zipfile.py", line 378, in _RealGetContents
    raise BadZipfile, "File is not a zip file"
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.

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.

What would cause a zip file to not have a table of contents?  Is there
some way I can add a table of contents to a zip file using python?
Maybe there is some more fundamental problem with the data that is
making it seem like there is no table of contents?

Thanks in advance for your help.
Ryan



More information about the Python-list mailing list