From 69bf903cdbe328c5aa8c88860adff8259ec458c4 Mon Sep 17 00:00:00 2001 From: Glavo Date: Sat, 2 Aug 2025 19:19:52 +0800 Subject: [PATCH] Update CompressingUtils --- .../hmcl/util/io/CompressingUtils.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/CompressingUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/CompressingUtils.java index b9ba5da3c..7ff6209f4 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/CompressingUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/CompressingUtils.java @@ -66,7 +66,7 @@ public final class CompressingUtils { cd.reset(); byte[] ba = entry.getRawName(); - int clen = (int)(ba.length * cd.maxCharsPerByte()); + int clen = (int) (ba.length * cd.maxCharsPerByte()); if (clen == 0) continue; if (clen <= cb.capacity()) cb.clear(); @@ -129,7 +129,19 @@ public final class CompressingUtils { } public static ZipArchiveReader openZipFile(Path zipFile) throws IOException { - return new ZipArchiveReader(Files.newByteChannel(zipFile)); + ZipArchiveReader zipReader = new ZipArchiveReader(Files.newByteChannel(zipFile)); + Charset suitableEncoding; + try { + suitableEncoding = findSuitableEncoding(zipReader); + if (suitableEncoding == StandardCharsets.UTF_8) + return zipReader; + } catch (Throwable e) { + IOUtils.closeQuietly(zipReader, e); + throw e; + } + + zipReader.close(); + return new ZipArchiveReader(Files.newByteChannel(zipFile), suitableEncoding); } public static ZipArchiveReader openZipFile(Path zipFile, Charset charset) throws IOException { @@ -221,9 +233,9 @@ public final class CompressingUtils { * Read the text content of a file in zip. * * @param zipFile the zip file - * @param name the location of the text in zip file, something like A/B/C/D.txt - * @throws IOException if the file is not a valid zip file. + * @param name the location of the text in zip file, something like A/B/C/D.txt * @return the plain text content of given file. + * @throws IOException if the file is not a valid zip file. */ public static String readTextZipEntry(File zipFile, String name) throws IOException { try (ZipArchiveReader s = new ZipArchiveReader(zipFile.toPath())) { @@ -235,9 +247,9 @@ public final class CompressingUtils { * Read the text content of a file in zip. * * @param zipFile the zip file - * @param name the location of the text in zip file, something like A/B/C/D.txt - * @throws IOException if the file is not a valid zip file. + * @param name the location of the text in zip file, something like A/B/C/D.txt * @return the plain text content of given file. + * @throws IOException if the file is not a valid zip file. */ public static String readTextZipEntry(ZipArchiveReader zipFile, String name) throws IOException { return IOUtils.readFullyAsString(zipFile.getInputStream(zipFile.getEntry(name))); @@ -247,9 +259,9 @@ public final class CompressingUtils { * Read the text content of a file in zip. * * @param zipFile the zip file - * @param name the location of the text in zip file, something like A/B/C/D.txt - * @throws IOException if the file is not a valid zip file. + * @param name the location of the text in zip file, something like A/B/C/D.txt * @return the plain text content of given file. + * @throws IOException if the file is not a valid zip file. */ public static String readTextZipEntry(Path zipFile, String name, Charset encoding) throws IOException { try (ZipArchiveReader s = openZipFile(zipFile, encoding)) {