fix(货源): 结果文件写 xlsx 的输出流补 64KB 缓冲,万级图片导出省约 7 秒

WpsCellImageWriter / ExcelCellImageWriter 都直接把 ZipOutputStream 套在
Files.newOutputStream 上,没有缓冲:每个 entry 的 local header、数据、以及 close() 时
逐条写的中央目录记录都是独立 syscall。10k 图片实测 media loop 2.97s + close 4.07s,
而 patchSheet 只有 0.19s——时间花在写放大,不在压缩。

现网确实吃这段:主机A 近 24h 的 [wps-dispimg] 日志里 images= 最大到 8058。

zip 是纯顺序写,加缓冲不改变产物字节。同一用例从 10.4~12.4s(贴穿测试里那个 10s 墙钟预算)
降到 1.81s,测试阈值一个字没动。

JPEG entry 仍走 DEFLATE(改 STORED 会变产物字节,需要真图 + WPS/Excel 打开回归,另议)。
This commit is contained in:
2026-09-28 18:46:01 +08:00
parent 31d3c0857d
commit ff3da2557b
2 changed files with 10 additions and 2 deletions
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.similarasin.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@@ -82,7 +83,10 @@ public final class ExcelCellImageWriter {
int patchedCells = -1;
try {
try (ZipFile zfIn = new ZipFile(xlsxPath.toFile());
ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(tmp))) {
// 与 WpsCellImageWriter 同一处修法:每个 entry 的头/数据/中央目录项都是独立 syscall,
// 万级图片时写放大占大头(实测 media loop 3.0s + close 4.1s)
ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(
Files.newOutputStream(tmp), 64 * 1024))) {
Enumeration<? extends ZipEntry> entries = zfIn.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.similarasin.util;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@@ -134,7 +135,10 @@ public final class WpsCellImageWriter {
// 校验,会抛 "ZipException: invalid entry size (expected 0 but got N bytes)"。ZipFile 直接读中央目录
// 拿到真实 size,绕开该问题。
try (ZipFile zfIn = new ZipFile(xlsxPath.toFile());
ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(tmp))) {
// 64KB 缓冲:一次导出常有 8k 级图片,未缓冲时每个 entry 的头/数据/中央目录项都是独立
// syscall(实测 10k entry 时 media loop 3.0s + close 4.1s,其中绝大部分是写放大不是压缩)
ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(
Files.newOutputStream(tmp), 64 * 1024))) {
Enumeration<? extends ZipEntry> entries = zfIn.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();