I used StringIO to create a file object that holds the xml data, then I created a gzip file with this file object, I am having trouble making this stream over HTTP with django, the file size is not always fixed and sometime it can be big, that is why I oppted for the HTTPStream instead of a normale HTTP response. I can't alos figure out how to send the file lenght, because the file object is not seekable.
Thank you for your help, cheers !
Here is my code:
# Get the XML string
xml_string = get_xml_cebd(cebds)
# Write the xml string to the string io holder
cebd_xml_file.write(xml_string)
# Flush the xml file buffer
cebd_xml_file.flush()
# Create the Gzip file handler, with the StringIO in the fileobj
gzip_handler = gzip.GzipFile(
fileobj=cebd_xml_file,
filename=base_file_name + '.xml',
mode='wb'
)
# Write the XML data into the gziped file
gzip_handler.write(cebd_xml_file.getvalue())
gzip_handler.flush()
# Generate the response using the file warpper, content type: x-gzip
# Since this file can be big, best to use the StreamingHTTPResponse
# that way we can garantee that the file will be sent entirely
response = StreamingHttpResponse(
gzip_handler,
content_type='application/x-gzip'
)
# Add content disposition so the browser will download the file
response['Content-Disposition'] = ('attachment; filename=' +
base_file_name + '.xml.gz')
# Content size
gzip_handler.seek(0, os.SEEK_END)
response['Content-Length'] = gzip_handler.tell()