我使用ByteArrayOutputStream和ZipOutPutSteam創建了一個zip文件但是它在創建大約200mb附近的大型zip文件時需要很多延遲。有沒有什麼有效的方法?從ZipOutPutStream使用Jax-rs下載Zip文件
URL u = new URL(url);
URLConnection uc = u.openConnection();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(out);
zipOut.putNextEntry(new ZipEntry("file.csv"));
zipOut.write(IOUtils.toByteArray(uc.getInputStream()));
uc.getInputStream().close();
zipOut.closeEntry();
zipOut.close();
ResponseBuilder response = Response.ok(out.toByteArray());
response.header("Content-Disposition", "attachment;
filename=\"LMPeriod.zip\"");
return response.build();
你可以嘗試使用壓縮級別。您可以預先壓縮文件。即有一些工作在後臺進行壓縮。您可以緩存壓縮文件。對於1kB數據+從文件系統讀取數據,壓縮本身仍然需要大約2微秒。使用您的解決方案(將整個文件讀入內存並壓縮到內存中),如果有多個併發下載請求,可能會遇到內存問題。 – andih