From ffd3cfe54a2ef3d226fb3c563c4a36101c1718ae Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 6 Feb 2015 13:17:15 -0600 Subject: [PATCH] Zip strategy roughed in --- src/Engines/CsvEngine.php | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/Engines/CsvEngine.php b/src/Engines/CsvEngine.php index 259c70c..12ee74b 100644 --- a/src/Engines/CsvEngine.php +++ b/src/Engines/CsvEngine.php @@ -5,11 +5,13 @@ use Quorum\Exporter\DataSheet; use Quorum\Exporter\EngineInterface; use Quorum\Exporter\Exceptions\ExportException; +use ZipStream\ZipStream; class CsvEngine implements EngineInterface { const STRATEGY_CONCAT = 'stat-concat'; const STRATEGY_ZIP = 'stat-zip'; + /** * @var resource[] */ @@ -144,7 +146,6 @@ public function processSheet( DataSheet $sheet ) { */ public function outputToStream( $outputStream ) { - switch( $this->multiSheetStrategy ) { case self::STRATEGY_ZIP: $tmpDir = rtrim($this->tmpDir ?: sys_get_temp_dir(), '/'); @@ -152,30 +153,18 @@ public function outputToStream( $outputStream ) { throw new \RuntimeException("Temporary Directory Not Found"); } - $tmpName = tempnam($tmpDir, $this->tmpPrefix); - - $zip = new \ZipArchive; - if( !$zip->open($tmpName, \ZipArchive::CREATE) ) { - throw new ExportException('Error creating zip'); - } + $zip = new ZipStream(null, [ ZipStream::OPTION_OUTPUT_STREAM => $outputStream ]); - $x = 0; foreach( $this->streams as $stream ) { rewind($stream); - $zip->addFromString('Sheet' . ($this->autoIndex++) . '.csv', stream_get_contents($stream)); - } - - $zip->close(); + $tmpStream = fopen("php://temp", "r+"); + fwrite($tmpStream, $this->getBom()); + stream_copy_to_stream($stream, $tmpStream); - $tmpStream = fopen($tmpName, 'r'); - stream_copy_to_stream($tmpStream, $outputStream); - fclose($tmpStream); + $zip->addFileFromStream('Sheet' . ($this->autoIndex++) . '.csv', $tmpStream); + } - register_shutdown_function(function () use ( $tmpName ) { - if( file_exists($tmpName) ) { - unlink($tmpName); - } - }); + $zip->finish(); break; case self::STRATEGY_CONCAT: