Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 16th, 2006, 04:55 PM
Waguy
Guest
 
Posts: n/a
Default zipfile decompress problems

Hi all,

I am new to python and want to create a process to unzip large numbers of
zip files I get from a SOAP application. The files all have a ZIP extention
and can be unzipped using WinZip.

However when I try opening the files using zlib or zipfile modules I get the
following error:

Traceback (most recent call last):
File "<pyshell#88>", line 1, in -toplevel-
file = zipfile.ZipFile("c:\\chessy.zip", "r")
File "C:\Python24\lib\zipfile.py", line 210, in __init__
self._GetContents()
File "C:\Python24\lib\zipfile.py", line 230, in _GetContents
self._RealGetContents()
File "C:\Python24\lib\zipfile.py", line 242, in _RealGetContents
raise BadZipfile, "File is not a zip file"
BadZipfile: File is not a zip file

The code I used in python was:

import zipfile
file = zipfile.ZipFile("c:\\chessy.zip", "r")

I tried to do the same with a zip file I created using WinZip and it worked
fine. I have been searching the web for about two days and haven't found
anything helpful.

Can anyone help with this? I have the zip I can send, however I tried to
send this message with it attached and it didn't get posted.

Cheers,




  #2  
Old January 16th, 2006, 07:35 PM
Giovanni Bajo
Guest
 
Posts: n/a
Default Re: zipfile decompress problems

Waguy wrote:
[color=blue]
> import zipfile
> file = zipfile.ZipFile("c:\\chessy.zip", "r")[/color]


Use "rb".
--
Giovanni Bajo


  #3  
Old January 16th, 2006, 07:35 PM
Waguy
Guest
 
Posts: n/a
Default Re: zipfile decompress problems

I tried that to and it didn't work, got the same message

Thanks though,



"Giovanni Bajo" <raNOsky@deveSPAMler.com> wrote in message
news:dqgrng$t2o$1@nnrp.ngi.it...[color=blue]
> Waguy wrote:
>[color=green]
>> import zipfile
>> file = zipfile.ZipFile("c:\\chessy.zip", "r")[/color]
>
>
> Use "rb".
> --
> Giovanni Bajo
>
>[/color]


  #4  
Old January 16th, 2006, 07:45 PM
Giovanni Bajo
Guest
 
Posts: n/a
Default Re: zipfile decompress problems

Waguy wrote:
[color=blue]
> I tried that to and it didn't work, got the same message
> Thanks though,[/color]


Can you send / provide a link to a minimal zip file which reproduces the
problem?
--
Giovanni Bajo


  #5  
Old January 17th, 2006, 07:55 AM
Carl Banks
Guest
 
Posts: n/a
Default Re: zipfile decompress problems


Waguy wrote:[color=blue]
> Hi all,
>
> I am new to python and want to create a process to unzip large numbers of
> zip files I get from a SOAP application. The files all have a ZIP extention
> and can be unzipped using WinZip.
>
> However when I try opening the files using zlib or zipfile modules I get the
> following error:
>
> Traceback (most recent call last):
> File "<pyshell#88>", line 1, in -toplevel-
> file = zipfile.ZipFile("c:\\chessy.zip", "r")
> File "C:\Python24\lib\zipfile.py", line 210, in __init__
> self._GetContents()
> File "C:\Python24\lib\zipfile.py", line 230, in _GetContents
> self._RealGetContents()
> File "C:\Python24\lib\zipfile.py", line 242, in _RealGetContents
> raise BadZipfile, "File is not a zip file"
> BadZipfile: File is not a zip file
>
> The code I used in python was:
>
> import zipfile
> file = zipfile.ZipFile("c:\\chessy.zip", "r")
>
> I tried to do the same with a zip file I created using WinZip and it worked
> fine. I have been searching the web for about two days and haven't found
> anything helpful.
>
> Can anyone help with this? I have the zip I can send, however I tried to
> send this message with it attached and it didn't get posted.
>
> Cheers,[/color]

I'm guessing the downloaded zip files have a faulty magic number or an
odd value that trips up the zipfile module but that winzip ignores.

Another possibility is this: From the documentation:

"This module does not currently handle ZIP files which have appended
comments, or multi-disk ZIP files."

One thing I've noticed is that, when downloading zip files from a
service of some sort, they often seem to have appended comments.
("This file downloaded from www.extremezipfiles.com, blah blah blah.")

Carl Banks

  #6  
Old January 19th, 2006, 10:35 PM
Scott David Daniels
Guest
 
Posts: n/a
Default Re: zipfile decompress problems

Carl Banks wrote:[color=blue]
> Waguy wrote:[color=green]
>> I am new to python and want to create a process to unzip large numbers of
>> zip files I get from a SOAP application. The files all have a ZIP extention
>> and can be unzipped using WinZip....
>> Can anyone help with this? ...[/color]
>
> Another possibility is this: From the documentation:
> "This module does not currently handle ZIP files which have appended
> comments, or multi-disk ZIP files."
>
> One thing I've noticed is that, when downloading zip files from a
> service of some sort, they often seem to have appended comments.
> ("This file downloaded from www.extremezipfiles.com, blah blah blah.")[/color]

Based on the file Waguy originally sent, you could try using this:

import zipfile, cStringIO
def getzip(filename, ignoreable=100):
try:
return zipfile.ZipFile(filename)
except zipfile.BadZipfile:
original = open(filename, 'rb')
try:
data = original.read()
finally:
original.close()
position = data.rindex(zipfile.stringEndArchive,
-(22 + ignoreable), -20)
coredata = cStringIO.StringIO(data[: 22 + position])
return zipfile.ZipFile(coredata)

--Scott David Daniels
scott.daniels@acm.org
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles