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.
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?
EDIT:
To elaborate on my response to Oben Sonne below, I'll have a function such as:
def iter_file(f,chunk=32768): return iter(lambda: f.read(chunk), '')
Which will let me specify a chunk size to return from the file when returning it to the WSGI server.
Then it's a simple matter of:
return iter_file(subprocess.Popen(["tar", "-Ocz"] + files, stdout=subprocess.PIPE).stdout)
or, if I want to return a file:
return iter_file(open(filename, "rb"))