Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : Generating a gzip string by returning chunks

Tags:

python

django

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.

like image 960
Steve K Avatar asked Jun 09 '26 14:06

Steve K


1 Answers

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)
like image 57
Dan D. Avatar answered Jun 12 '26 03:06

Dan D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!