Update CompressingUtils

This commit is contained in:
Glavo 2025-08-02 19:19:52 +08:00
parent 6c3a924db7
commit 69bf903cdb

View File

@ -66,7 +66,7 @@ public final class CompressingUtils {
cd.reset(); cd.reset();
byte[] ba = entry.getRawName(); byte[] ba = entry.getRawName();
int clen = (int)(ba.length * cd.maxCharsPerByte()); int clen = (int) (ba.length * cd.maxCharsPerByte());
if (clen == 0) continue; if (clen == 0) continue;
if (clen <= cb.capacity()) if (clen <= cb.capacity())
cb.clear(); cb.clear();
@ -129,7 +129,19 @@ public final class CompressingUtils {
} }
public static ZipArchiveReader openZipFile(Path zipFile) throws IOException { 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 { 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. * Read the text content of a file in zip.
* *
* @param zipFile the zip file * @param zipFile the zip file
* @param name the location of the text in zip file, something like A/B/C/D.txt * @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.
* @return the plain text content of given file. * @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 { public static String readTextZipEntry(File zipFile, String name) throws IOException {
try (ZipArchiveReader s = new ZipArchiveReader(zipFile.toPath())) { try (ZipArchiveReader s = new ZipArchiveReader(zipFile.toPath())) {
@ -235,9 +247,9 @@ public final class CompressingUtils {
* Read the text content of a file in zip. * Read the text content of a file in zip.
* *
* @param zipFile the zip file * @param zipFile the zip file
* @param name the location of the text in zip file, something like A/B/C/D.txt * @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.
* @return the plain text content of given file. * @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 { public static String readTextZipEntry(ZipArchiveReader zipFile, String name) throws IOException {
return IOUtils.readFullyAsString(zipFile.getInputStream(zipFile.getEntry(name))); 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. * Read the text content of a file in zip.
* *
* @param zipFile the zip file * @param zipFile the zip file
* @param name the location of the text in zip file, something like A/B/C/D.txt * @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.
* @return the plain text content of given file. * @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 { public static String readTextZipEntry(Path zipFile, String name, Charset encoding) throws IOException {
try (ZipArchiveReader s = openZipFile(zipFile, encoding)) { try (ZipArchiveReader s = openZipFile(zipFile, encoding)) {