Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"config": {
"platform": {
"php" : "8.1.99"
"php": "8.1.99"
},
"process-timeout": 600,
"sort-packages": true,
Expand Down Expand Up @@ -120,7 +120,8 @@
"autoload-dev": {
"psr-4": {
"PhpOffice\\PhpSpreadsheetTests\\": "tests/PhpSpreadsheetTests",
"PhpOffice\\PhpSpreadsheetInfra\\": "infra"
"PhpOffice\\PhpSpreadsheetInfra\\": "infra",
"PhpOffice\\PhpSpreadsheetBenchmarks\\": "tests/Benchmark"
}
}
}
35 changes: 34 additions & 1 deletion src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class Xlsx extends BaseReader

private bool $parseHuge = false;

/** @var array<string, string> Cache for zip archive entry contents, keyed by normalized file name */
private array $zipCache = [];

/**
* Allow use of LIBXML_PARSEHUGE.
* This option can lead to memory leaks and failures,
Expand Down Expand Up @@ -94,6 +97,7 @@ public function canRead(string $filename): bool
return false;
}

$this->clearZipCache();
$result = false;
$this->zip = $zip = new ZipArchive();

Expand All @@ -102,6 +106,7 @@ public function canRead(string $filename): bool
$result = !empty($workbookBasename);

$zip->close();
$this->clearZipCache();
}

return $result;
Expand Down Expand Up @@ -182,6 +187,7 @@ private function loadZipNonamespace(string $filename, string $ns): SimpleXMLElem
public function listWorksheetNames(string $filename): array
{
File::assertFile($filename, self::INITIAL_FILE);
$this->clearZipCache();

$worksheetNames = [];

Expand All @@ -207,6 +213,7 @@ public function listWorksheetNames(string $filename): array
}

$zip->close();
$this->clearZipCache();

return $worksheetNames;
}
Expand All @@ -219,6 +226,7 @@ public function listWorksheetNames(string $filename): array
public function listWorksheetInfo(string $filename): array
{
File::assertFile($filename, self::INITIAL_FILE);
$this->clearZipCache();

$worksheetInfo = [];

Expand Down Expand Up @@ -315,6 +323,7 @@ public function listWorksheetInfo(string $filename): array
}

$zip->close();
$this->clearZipCache();

return $worksheetInfo;
}
Expand Down Expand Up @@ -400,6 +409,8 @@ private function fileExistsInArchive(ZipArchive $archive, string $fileName = '')

private function getFromZipArchive(ZipArchive $archive, string $fileName = ''): string
{
assert($archive === $this->zip, 'Cache assumes all reads use the same archive');

// Root-relative paths
if (str_contains($fileName, '//')) {
$fileName = substr($fileName, strpos($fileName, '//') + 1);
Expand All @@ -409,6 +420,11 @@ private function getFromZipArchive(ZipArchive $archive, string $fileName = ''):
$fileName = Preg::replace('/^\.\//', '', $fileName);
$fileName = File::realpath($fileName);

// Return from cache if available
if (isset($this->zipCache[$fileName])) {
return $this->zipCache[$fileName];
}

// Sadly, some 3rd party xlsx generators don't use consistent case for filenaming
// so we need to load case-insensitively from the zip file

Expand All @@ -424,7 +440,22 @@ private function getFromZipArchive(ZipArchive $archive, string $fileName = ''):
$contents = $archive->getFromName(str_replace('/', '\\', $fileName), 0, ZipArchive::FL_NOCASE);
}

return ($contents === false) ? '' : $contents;
$result = ($contents === false) ? '' : $contents;

// Cache the result for subsequent reads of the same entry
$this->zipCache[$fileName] = $result;

return $result;
}

/**
* Clear the zip archive read cache.
* Called at the start of each load operation to ensure
* stale data from a previous load does not persist.
*/
private function clearZipCache(): void
{
$this->zipCache = [];
}

/**
Expand All @@ -433,6 +464,7 @@ private function getFromZipArchive(ZipArchive $archive, string $fileName = ''):
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
{
File::assertFile($filename, self::INITIAL_FILE);
$this->clearZipCache();

// Initialisations
$excel = $this->newSpreadsheet();
Expand Down Expand Up @@ -2051,6 +2083,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$excel->setUnparsedLoadedData($unparsedLoadedData);

$zip->close();
$this->clearZipCache();

return $excel;
}
Expand Down
Loading
Loading