Skip to content

Commit

Permalink
Zip strategy roughed in
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Feb 6, 2015
1 parent 32359ce commit ffd3cfe
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/Engines/CsvEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down Expand Up @@ -144,38 +146,25 @@ public function processSheet( DataSheet $sheet ) {
*/
public function outputToStream( $outputStream ) {


switch( $this->multiSheetStrategy ) {
case self::STRATEGY_ZIP:
$tmpDir = rtrim($this->tmpDir ?: sys_get_temp_dir(), '/');
if( !is_dir($tmpDir) ) {
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:
Expand Down

0 comments on commit ffd3cfe

Please sign in to comment.