应付款终版导入:流式读取改用 zip:// 管道,解析内存 274MB→76MB #362

Merged
renshen merged 1 commits from feature/sumu-import-stream-memory into main 2026-10-17 00:05:06 +08:00

View File

@ -89,21 +89,12 @@ class PayablesWorkbookStreamReader
return;
}
$zip = new ZipArchive();
if ($zip->open($this->path) !== true) {
throw new RuntimeException('无法打开工作簿。');
$reader = new XMLReader();
if (! $reader->open('zip://' . $this->path . '#' . $path, null, LIBXML_NONET)) {
throw new RuntimeException('无法打开工作表。');
}
try {
$stream = $zip->getStream($path);
if ($stream === false) {
return;
}
$reader = new XMLReader();
$reader->XML(stream_get_contents($stream), null, LIBXML_NONET);
fclose($stream);
$dictionary = $this->xlsxSharedStrings();
$rowNumber = 0;
@ -125,7 +116,7 @@ class PayablesWorkbookStreamReader
yield $rowNumber => $this->readRow($reader, $dictionary);
}
} finally {
$zip->close();
$reader->close();
}
}
@ -467,21 +458,13 @@ class PayablesWorkbookStreamReader
return [$this->sharedStrings, $this->sharedStringOffsets];
}
$zip = new ZipArchive();
if ($zip->open($this->path) !== true) {
throw new RuntimeException('无法打开工作簿。');
$blob = '';
$reader = new XMLReader();
if (! @$reader->open('zip://' . $this->path . '#xl/sharedStrings.xml', null, LIBXML_NONET)) {
return [$this->sharedStrings = '', $this->sharedStringOffsets = new SplFixedArray(1)];
}
$blob = '';
try {
$xml = $zip->getFromName('xl/sharedStrings.xml');
if ($xml === false) {
return [$this->sharedStrings = '', $this->sharedStringOffsets = new SplFixedArray(1)];
}
$reader = new XMLReader();
$reader->XML($xml, null, LIBXML_NONET);
$count = 0;
$offsets = new SplFixedArray(1024);
$offsets[0] = 0;
@ -517,10 +500,9 @@ class PayablesWorkbookStreamReader
}
}
$reader->close();
$offsets->setSize($count + 1);
} finally {
$zip->close();
$reader->close();
}
return [$this->sharedStrings = $blob, $this->sharedStringOffsets = $offsets];