Tarring files together as iterable in Python?
<p>I'm running a WSGI server and part of the API I'm writing returns some (rather large) files along with meta-data about them.  I'd like to tar/gzip the files together both to conserve bandwidth and so only one file has to be downloaded.  Since WSGI lets you return an iterable object, I'd like to return an iterable that returns chunks of the tar.gz file as it's produced.  </p>

<p>My question is what's a good way to tar/gzip files together in Python in a way that's amenable to streaming the output back to the user?</p>

<p>EDIT:</p>

<p>To elaborate on my response to Oben Sonne below, I'll have a function such as:</p>

<pre><code>def iter_file(f,chunk=32768): return iter(lambda: f.read(chunk), '')
</code></pre>

<p>Which will let me specify a chunk size to return from the file when returning it to the WSGI server.</p>

<p>Then it's a simple matter of:</p>

<pre><code>return iter_file(subprocess.Popen(["tar", "-Ocz"] + files, stdout=subprocess.PIPE).stdout)
</code></pre>

<p>or, if I want to return a file: </p>

<pre><code>return iter_file(open(filename, "rb"))
</code></pre>
