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();
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)) {