Unzipping a .zip properly, and from a remote URL

M.-A. Lemburg mal at egenix.com
Wed Feb 4 22:43:04 CET 2009


On 2009-02-03 15:32, Tino Wildenhain wrote:
> Christopher Culver wrote:
>> Tino Wildenhain <tino at wildenhain.de> writes:
>>> so instead you would use archive = zipfile.ZipFile(remotedata)
>>
>> That produces the following error if I try that in the Python
>> interpreter (URL edited for privacy):
>>
>>>>> import zipfile
>>>>> import urllib2
>>>>> remotedata = urllib2.urlopen("http://...file.zip")
>>>>> archive = zipfile.ZipFile(remotedata)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>>   File "/usr/lib/python2.5/zipfile.py", line 346, in __init__
>>     self._GetContents()
>>   File "/usr/lib/python2.5/zipfile.py", line 366, in _GetContents
>>     self._RealGetContents()
>>   File "/usr/lib/python2.5/zipfile.py", line 376, in _RealGetContents
>>     endrec = _EndRecData(fp)
>>   File "/usr/lib/python2.5/zipfile.py", line 133, in _EndRecData
>>     fpin.seek(-22, 2)               # Assume no archive comment.
>> AttributeError: addinfourl instance has no attribute 'seek'

Try this:

>>> import urllib, zipfile, cStringIO
>>> zipwebfile = urllib.urlopen('http://downloads.egenix.com/python/locale-0.1.zip')
>>> buffer = cStringIO.StringIO(zipwebfile.read())
>>> zfile = zipfile.ZipFile(buffer)
>>> zfile.printdir()
File Name                                             Modified             Size
locale/Makefile.pre.in                         1997-10-31 21:13:06         9818
locale/Setup.in                                1997-10-31 21:14:04           74
locale/locale.c                                1997-11-19 17:36:46         4698
locale/CommandLine.py                          1997-11-19 15:50:02         2306
locale/probe.py                                1997-11-19 15:51:18         1870
locale/__init__.py                             1997-11-19 17:55:02            0

The trick is to use a StringIO buffer to provide the .seek()
method.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 04 2009)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-list mailing list