title: Download and decompress gzipped file in memory?
body: <p>I would like to download a file using urllib and decompress the file in memory before saving.</p>
  
  <p>This is what I have right now:</p>
  
  <pre class="lang-py prettyprint-override"><code>response = urllib2.urlopen(baseURL + filename)
  compressedFile = StringIO.StringIO()
  compressedFile.write(response.read())
  decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb')
  outfile = open(outFilePath, 'w')
  outfile.write(decompressedFile.read())
  </code></pre>
  
  <p>This ends up writing empty files. How can I achieve what I'm after?</p>
  
  <p><strong>Updated Answer:</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>#! /usr/bin/env python2
  import urllib2
  import StringIO
  import gzip
  
  baseURL = "https://www.kernel.org/pub/linux/docs/man-pages/"        
  # check filename: it may change over time, due to new updates
  filename = "man-pages-5.00.tar.gz" 
  outFilePath = filename[:-3]
  
  response = urllib2.urlopen(baseURL + filename)
  compressedFile = StringIO.StringIO(response.read())
  decompressedFile = gzip.GzipFile(fileobj=compressedFile)
  
  with open(outFilePath, 'w') as outfile:
      outfile.write(decompressedFile.read())
  </code></pre>