If you need to compress data and send it as "Content-disposition: attachment" and on-the-fly to the client due to the size for example (40Mb) here's a dirty trick using ob_gzhandler()
Keep in mind that $str is the content to output.
When you start the output call
<?php
echo ob_gzhandler($str, PHP_OUTPUT_HANDLER_START);
?>
Then to output any further content
<?php
echo ob_gzhandler($str, PHP_OUTPUT_HANDLER_CONT);
?>
And to close the output
<?php
echo ob_gzhandler('', PHP_OUTPUT_HANDLER_END);
exit;
?>
Only tested on Apache 1.3.33 with PHP 5.0.4