Skip to content

Commit

Permalink
fix zip usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Feb 7, 2024
1 parent 1f6c041 commit ae6dabf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/SpreadCompat.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@ public static function getAdapterForFile(string $filename, string $ext = null):
}

/**
* @param resource $stream
* @return string
*/
public static function getTempFilename($stream = null): string
public static function getTempFilename(): string
{
if ($stream === null) {
$stream = tmpfile();
if (!$stream) {
throw new RuntimeException("Could not get temp file");
}
$result = tempnam(sys_get_temp_dir(), 'S_C'); // windows only use the 3 first letters
if ($result === false) {
throw new Exception("Unable to create temp file");
}
$filename = stream_get_meta_data($stream)['uri'];
return $filename;
return $result;
}

public static function isTempFile(string $file): bool
{
return str_starts_with(basename($file), 'S_C');
}

/**
Expand Down
34 changes: 30 additions & 4 deletions src/Xlsx/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ protected function write($zip, iterable $data): void
$stream = $this->genWorksheet($data, $memory);
rewind($stream);
if ($zip instanceof ZipArchive) {
$zip->addFile(SpreadCompat::getTempFilename($stream), $path);
// $zip->addFile(SpreadCompat::getTempFilename($stream), $path);
$contents = stream_get_contents($stream);
if ($contents) {
$zip->addFromString($contents, $path);
}
} else {
$zip->addFileFromStream('xl/worksheets/sheet1.xml', $stream);
}
Expand Down Expand Up @@ -362,18 +366,40 @@ public function writeFile(
$this->write($zip, $data);
$size = $zip->finish();
} else {
$mode = ZipArchive::CREATE;
if (is_file($filename)) {
unlink($filename); // ZipArchive needs no file
$mode = ZipArchive::OVERWRITE;
}
$zip = new ZipArchive();
$zip->open($filename, ZipArchive::CREATE);
$result = $zip->open($filename, $mode);
if ($result !== true) {
throw new Exception("Failed to open zip archive, code: " . self::zipError($result));
}
$this->write($zip, $data);
$zip->close();
if (!SpreadCompat::isTempFile($filename)) {
$zip->close();
}
}

return fclose($stream);
}

protected static function zipError(int $code): string
{
return match ($code) {
ZipArchive::ER_EXISTS => 'File already exists.',
ZipArchive::ER_INCONS => 'Zip archive inconsistent.',
ZipArchive::ER_INVAL => 'Invalid argument.',
ZipArchive::ER_MEMORY => 'Malloc failure.',
ZipArchive::ER_NOENT => 'No such file.',
ZipArchive::ER_NOZIP => 'Not a zip archive.',
ZipArchive::ER_OPEN => 'Can\'t open file.',
ZipArchive::ER_READ => 'Read error.',
ZipArchive::ER_SEEK => 'Seek error.',
default => 'Unknown error code ' . $code . '.',
};
}

public function output(
iterable $data,
string $filename,
Expand Down

0 comments on commit ae6dabf

Please sign in to comment.