From d47de0609ce663c945eb4b6b0b4b89f0283cd17f Mon Sep 17 00:00:00 2001 From: RhiobeT Date: Sun, 12 Apr 2026 20:37:55 +0200 Subject: [PATCH] Improve zip resources handling --- .../rhiobet/lalafin/file/FileInfoService.java | 3 +- .../lalafin/file/FileServeService.java | 62 ++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/main/java/sh/rhiobet/lalafin/file/FileInfoService.java b/src/main/java/sh/rhiobet/lalafin/file/FileInfoService.java index ef7e0d7..e4ba7a8 100644 --- a/src/main/java/sh/rhiobet/lalafin/file/FileInfoService.java +++ b/src/main/java/sh/rhiobet/lalafin/file/FileInfoService.java @@ -224,8 +224,7 @@ public class FileInfoService { archiveInfo.viewUrl = "/view" + requestedUri + "/1"; Path zipPath = Paths.get(fileApiConfiguration.directory() + "/file/" + requestedPath); - try { - ZipFile zipFile = new ZipFile(zipPath.toFile()); + try (ZipFile zipFile = new ZipFile(zipPath.toFile())) { zipFile.stream().filter(e -> !e.isDirectory()).forEach(e -> { String zipEntryName = e.getName(); String zipEntryNameUri = diff --git a/src/main/java/sh/rhiobet/lalafin/file/FileServeService.java b/src/main/java/sh/rhiobet/lalafin/file/FileServeService.java index f6661fd..26eae8e 100644 --- a/src/main/java/sh/rhiobet/lalafin/file/FileServeService.java +++ b/src/main/java/sh/rhiobet/lalafin/file/FileServeService.java @@ -112,13 +112,14 @@ public class FileServeService { ZipEntry zipEntry = zipFile.getEntry(name); if (zipEntry == null) { + zipFile.close(); throw new IOException(); } ResponseBuilder response; long fileSize = zipEntry.getSize(); - response = Response.ok(zipFile.getInputStream(zipEntry)); + response = Response.ok(new ZipEntryInputStream(zipFile, zipFile.getInputStream(zipEntry))); response.header("Content-Length", Long.toString(fileSize)); response.header("Accept-Ranges", "bytes"); @@ -221,3 +222,62 @@ class FileServeInputStream extends InputStream { } } } + +class ZipEntryInputStream extends InputStream { + private final ZipFile zipFile; + private final InputStream is; + + public ZipEntryInputStream(ZipFile zipFile, InputStream is) { + this.zipFile = zipFile; + this.is = is; + } + + @Override + public int read() throws IOException { + return this.is.read(); + } + + @Override + public int read(byte[] buffer) throws IOException { + return this.is.read(buffer); + } + + @Override + public int read(byte[] buffer, int off, int len) throws IOException { + return this.is.read(buffer, off, len); + } + + @Override + public byte[] readAllBytes() throws IOException { + return this.is.readAllBytes(); + } + + @Override + public byte[] readNBytes(int len) throws IOException { + return this.is.readNBytes(len); + } + + @Override + public int readNBytes(byte[] buf, int off, int len) throws IOException { + return this.is.readNBytes(buf, off, len); + } + + @Override + public long skip(long n) throws IOException { + return this.is.skip(n); + } + + @Override + public int available() throws IOException { + return this.is.available(); + } + + @Override + public void close() throws IOException { + try { + this.is.close(); + } finally { + this.zipFile.close(); + } + } +}