title: Python: Creating a streaming gzip'd file-like?
body: <p>I'm trying to figure out the best way to compress a stream with Python's <code>zlib</code>.</p>
  
  <p>I've got a file-like input stream (<code>input</code>, below) and an output function which accepts a file-like (<code>output_function</code>, below):</p>
  
  <pre><code>with open("file") as input:
      output_function(input)
  </code></pre>
  
  <p>And I'd like to gzip-compress <code>input</code> chunks before sending them to <code>output_function</code>:</p>
  
  <pre><code>with open("file") as input:
      output_function(gzip_stream(input))
  </code></pre>
  
  <p>It looks like the <a href="http://docs.python.org/library/gzip.html#module-gzip" rel="noreferrer">gzip</a> module assumes that either the input or the output will be a gzip'd file-on-disk… So I assume that the <a href="http://docs.python.org/library/zlib.html#module-zlib" rel="noreferrer">zlib</a> module is what I want.</p>
  
  <p>However, it doesn't natively offer a simple way to create a stream file-like… And the stream-compression it does support comes by way of manually adding data to a compression buffer, then flushing that buffer.</p>
  
  <p>Of course, I could write a wrapper around <code>zlib.Compress.compress</code> and <code>zlib.Compress.flush</code> (<code>Compress</code> is returned by <code>zlib.compressobj()</code>), but I'd be worried about getting buffer sizes wrong, or something similar.</p>
  
  <p>So, what's the simplest way to create a streaming, gzip-compressing file-like with Python?</p>
  
  <p><strong>Edit</strong>: To clarify, the input stream and the compressed output stream are both too large to fit in memory, so something like <code>output_function(StringIO(zlib.compress(input.read())))</code> doesn't really solve the problem.</p>