Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > BadZipfile "file is not a zip file"

Reply
Thread Tools

BadZipfile "file is not a zip file"

 
 
webcomm
Guest
Posts: n/a
 
      01-09-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
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      01-09-2009
webcomm wrote:
> 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?
>

You're just creating a file called "data.zip". That doesn't make it a
zip file. A zip file has a specific format. If the file doesn't have
that format then the zipfile module will complain.
 
Reply With Quote
 
 
 
 
webcomm
Guest
Posts: n/a
 
      01-09-2009
On Jan 8, 8:02*pm, MRAB <(E-Mail Removed)> wrote:
> You're just creating a file called "data.zip". That doesn't make it a
> zip file. A zip file has a specific format. If the file doesn't have
> that format then the zipfile module will complain.


Hmm. When I open it in Windows or with 7-Zip, it contains a text file
that has the data I would expect it to have. I guess that alone
doesn't necessarily prove it's a zip file?

datum is something I'm downloading via a web service. The providers
of the service say it's a zip file, and have provided a code sample in
C# (which I know nothing about) that shows how to deal with it. In
the code sample, the file is base64 decoded and then unzipped. I'm
trying to write something in Python to decode and unzip the file.

I checked the file for comments and it has none. At least, when I
view the properties in Windows, there are no comments.
 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      01-09-2009
On Fri, Jan 9, 2009 at 11:28 AM, webcomm <(E-Mail Removed)> wrote:
> Hmm. When I open it in Windows or with 7-Zip, it contains a text file
> that has the data I would expect it to have. I guess that alone
> doesn't necessarily prove it's a zip file?
>
> datum is something I'm downloading via a web service. The providers
> of the service say it's a zip file, and have provided a code sample in
> C# (which I know nothing about) that shows how to deal with it. In
> the code sample, the file is base64 decoded and then unzipped. I'm
> trying to write something in Python to decode and unzip the file.


Send us a sample of this file in question...

cheers
James
 
Reply With Quote
 
webcomm
Guest
Posts: n/a
 
      01-09-2009
On Jan 8, 8:39*pm, "James Mills" <(E-Mail Removed)> wrote:
> Send us a sample of this file in question...


It contains data that I can't share publicly. I could ask the
providers of the service if they have a dummy file I could use that
doesn't contain any real data, but I don't know how responsive they'll
be. It's an event registration service called RegOnline.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      01-09-2009
webcomm wrote:
> On Jan 8, 8:02 pm, MRAB <(E-Mail Removed)> wrote:
>> You're just creating a file called "data.zip". That doesn't make it
>> a zip file. A zip file has a specific format. If the file doesn't
>> have that format then the zipfile module will complain.

>
> Hmm. When I open it in Windows or with 7-Zip, it contains a text
> file that has the data I would expect it to have. I guess that alone
> doesn't necessarily prove it's a zip file?
>
> datum is something I'm downloading via a web service. The providers
> of the service say it's a zip file, and have provided a code sample
> in C# (which I know nothing about) that shows how to deal with it.
> In the code sample, the file is base64 decoded and then unzipped.
> I'm trying to write something in Python to decode and unzip the file.
>
> I checked the file for comments and it has none. At least, when I
> view the properties in Windows, there are no comments.
>

Ah, OK. You didn't explicitly say in your original posting that the
decoded data was definitely zipfile data. There was a thread a month ago
about gzip Unix commands which could also handle non-gzipped files and I
was wondering whether this problem was something like that. Have you
tried gzip instead?
 
Reply With Quote
 
webcomm
Guest
Posts: n/a
 
      01-09-2009
On Jan 8, 8:54*pm, MRAB <(E-Mail Removed)> wrote:
> Have you tried gzip instead?


There's no option to download the data in a gzipped format. The files
are .zip archives.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-09-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
 
Reply With Quote
 
Martin v. Lwis
Guest
Posts: n/a
 
      01-09-2009
> What would cause a zip file to not have a table of contents?

AFAICT, _EndRecData is failing to find the "end of zipfile" structure in
the file. You might want debug through it to see where it looks, and how
it decides that this structure is not present in the file. Towards
22 bytes before the end of the file, the bytes PK\005\006 should appear.
If they don't appear, you don't have a zipfile. If they appear, but
elsewhere towards the end of the file, there might be a bug in the
zip file module (or, more likely, the zip file uses an optional zip
feature which the module doesn't implement).

Regards,
Martin
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      01-09-2009
On Jan 9, 2:16*am, Steven D'Aprano <st...@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.



The zipfile format is kind of brain dead, you can't tell where the end
of the file is supposed to be by looking at the header. If the end of
file hasn't yet been reached there could be more data. To make
matters worse, somehow zip files came to have text comments simply
appended to the end of them. (Probably this was for the benefit of
people who would cat them to the terminal.)

Anyway, if you see something that doesn't adhere to the zipfile
format, you don't have any foolproof way to know if it's because the
file is corrupted or if it's just an appended comment.

Most zipfile readers use a heuristic to distinguish. Python's zipfile
module just assumes it's corrupted.

The following post from a while back gives a solution that tries to
snip the comment off so that zipfile module can handle it. It might
help you out.

http://groups.google.com/group/comp....008e48368c6543


Carl Banks
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Possibility to add a zip-file to a new zip-file with "add to zip" (right-click) ?? erikkie@casema.nl Computer Support 4 06-26-2006 12:18 AM
uploading and receiving zip file(java.util.zip) ? sunil_varma Java 1 02-06-2006 10:37 AM
java.util.zip.ZipException: error in opening zip file HARI OM Java 0 05-03-2005 05:02 PM
java.util.zip.ZipException: error in opening zip file hari_om@hotmail.com Java 0 05-03-2005 04:47 PM
java.util.zip - problem opening some legitimite me zip files Alex Hunsley Java 1 09-16-2004 02:06 PM



Advertisments