Created
March 4, 2009 20:41
-
-
Save pope/73994 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import gzip | |
| import zlib | |
| from urlparse import urlparse | |
| from httplib import HTTPConnection | |
| def workding_download_gzip_file(url, filename): | |
| """Download a gzip file and decompress it | |
| Save the gzip file, then read it in and re-save it | |
| """ | |
| o = urlparse(url) | |
| conn = HTTPConnection(o.netloc) | |
| conn.request("GET", o.path) | |
| resp = conn.getresponse() | |
| data = resp.read() | |
| gz_filename = filename + ".gz" | |
| with open(gz_filename, "wb") as gf: | |
| gf.write(data) | |
| gf = gzip.open(gz_filename, "rb") | |
| data = gf.read() | |
| gf.close() | |
| os.remove(gz_filename) | |
| with open(filename, "w") as f: | |
| f.write(data) | |
| def nonworkding_download_gzip_file(url, filename): | |
| """Download a gzip file and decompress it | |
| Try to decompress the zip from the http stream. This is just not working | |
| """ | |
| o = urlparse(url) | |
| conn = HTTPConnection(o.netloc) | |
| conn.request("GET", o.path) | |
| resp = conn.getresponse() | |
| data = resp.read() | |
| with open(filename, "w") as f: | |
| f.write(zlib.decompress(data)) | |
| if __name__ == "__main__": | |
| url = "http://shifteleven.com/dummy.txt.gz" | |
| workding_download_gzip_file(url, "working.txt") | |
| nonworkding_download_gzip_file(url, "nonworking.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment