I create a xml file based on information from my database (xmltv format). These xml files can be quite big - 25-70mb is normal. Now i create the xml file on the fly like this:
$xmlWriter = new XMLWriter();
$xmlWriter->openURI('php://output');
and flush through the loop to prevent memory overflow. I also set headers to push the content as download:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $config->filename . '.xml"');
I would like to be able to zip/gzip the xml because of the size. Is this possible on the fly? I have used PHPZip before, which works good with files, but i dont know if i can write the xml output directly to the zip?
If I have understood correctly, the goal is to create gzip compressed data dynamically, without creating a file on the server. This is possible with
deflate_initanddeflate_add, but requires PHP 7.With
deflate_addwe can add more data any number of times (the mode should beZLIB_FINISHfor the last chunk).We can adjust this method using
XMLWriter::openMemory(stores the data in memory) andXMLWriter::flush, to zip the xml elements as they are produced, and create the contents of a .gz file on the fly. For example this code:creates xml elements, compresses and outputs them one by one, without using too much memory or any files.