mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-09 20:06:39 -04:00
Update CompressingUtils
This commit is contained in:
parent
6c3a924db7
commit
69bf903cdb
@ -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 {
|
||||||
@ -222,8 +234,8 @@ public final class CompressingUtils {
|
|||||||
*
|
*
|
||||||
* @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())) {
|
||||||
@ -236,8 +248,8 @@ public final class CompressingUtils {
|
|||||||
*
|
*
|
||||||
* @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)));
|
||||||
@ -248,8 +260,8 @@ public final class CompressingUtils {
|
|||||||
*
|
*
|
||||||
* @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)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user