Unzipping a .zip properly, and from a remote URL

Scott David Daniels Scott.Daniels at Acm.Org
Tue Feb 3 11:05:20 EST 2009


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): ....
>>>> remotedata = urllib2.urlopen("http://...file.zip")
>>>> archive = zipfile.ZipFile(remotedata)

No, he means you need to pull down the remote data.  Zip  file reading
is not done in a single pass; it needs to seek to find the directory,
then again to get to the data for any particular file.  Seeking around
a file does not work so well across the net.

Try something like:
     >>> import zipfile
     >>> import urllib2
     >>> import StringIO # or cStringIO as StringIO
     >>> remotehandle = urllib2.urlopen("http://...file.zip")
     >>> zipdata = StringIO.StringIO(remotehandle.read())
     >>> remotehandle.close()
     >>> archive = zipfile.ZipFile(zipdata)
or:
     >>> with urllib2.urlopen("http://...file.zip") as remotehandle:
     ...    zipdata = StringIO.StringIO(remotehandle.read())
     ...    archive = zipfile.ZipFile(zipdata)

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list