Decompressing a file retrieved by URL seems too complex

Discussion in 'Python' started by John Nagle, Aug 12, 2010.

  1. John Nagle

    John Nagle Guest

    I'm reading a URL which is a .gz file, and decompressing
    it. This works, but it seems far too complex. Yet
    none of the "wrapping" you might expect to work
    actually does. You can't wrap a GzipFile around
    an HTTP connection, because GzipFile, reasonably enough,
    needs random access, and tries to do "seek" and "tell".
    Nor is the output descriptor from gzip general; it fails
    on "readline", but accepts "read". (No good reason
    for that.) So I had to make a second copy.

    John Nagle

    def readurl(url) :
    if url.endswith(".gz") :
    nd = urllib2.urlopen(url,timeout=TIMEOUTSECS)
    td1 = tempfile.TemporaryFile() # compressed file
    td1.write(nd.read()) # fetch and copy file
    nd.close() # done with network
    td2 = tempfile.TemporaryFile() # decompressed file
    td1.seek(0) # rewind
    gd = gzip.GzipFile(fileobj=td1, mode="rb") # wrap unzip
    td2.write(gd.read()) # decompress file
    td1.close() # done with compressed copy
    td2.seek(0) # rewind
    return(td2) # return file object for compressed object
    else :
    return(urllib2.urlopen(url,timeout=TIMEOUTSECS))
     
    John Nagle, Aug 12, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. I'm New to Java
    Replies:
    9
    Views:
    486
    Murray
    Jul 15, 2004
  2. Fredrik Lundh
    Replies:
    0
    Views:
    603
    Fredrik Lundh
    Dec 14, 2006
  3. SeanMon

    Decompressing gzip over FTP

    SeanMon, Aug 22, 2009, in forum: Python
    Replies:
    2
    Views:
    338
    Albert Hopkins
    Aug 22, 2009
  4. John Nagle
    Replies:
    2
    Views:
    317
  5. Ahmad Azizan
    Replies:
    2
    Views:
    256
    Brian Candler
    Mar 22, 2010
Loading...

Share This Page