I am going to ask for something I could not find on Stackoverflow. I am doing some Django, and I've recently discovered that I can stream the HTTP output using a generator. The output of a page is perfect for a normal case, however I wanted to stream the page output using GZip compression.
I've tried using the simple zlib.compress function, to no avail. The function generates small gzip files.
I want return small chunks of data as they are processed, as a string. Those chunks should form the content of a Gzipped file. How one would do this ? Thanks.
use zlib.compressobj([level]) and Compress.compress(string) and Compress.flush([mode]) to finish
import zlib
def compress(chunks):
c = zlib.compressobj()
for chunk in chunks:
yield c.compress(chunk)
yield c.flush(zlib.Z_FINISH)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With