打开 zip 文件时忽略 local file header (#6834)

This commit is contained in:
Glavo
2026-09-08 12:02:17 +08:00
committed by GitHub
parent 0f4152406c
commit e2d3ac847d
2 changed files with 5 additions and 20 deletions
@@ -19,16 +19,13 @@ package org.jackhuang.hmcl.util.io;
import kala.compress.archivers.zip.ZipArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import kala.compress.utils.BufferedDataInputSeekableChannel;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jackhuang.hmcl.util.tree.ZipFileTree;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.*;
import java.nio.file.*;
import java.nio.file.spi.FileSystemProvider;
@@ -131,18 +128,6 @@ public final class CompressingUtils {
throw new IOException("Cannot find suitable encoding for the zip.");
}
private static BufferedDataInputSeekableChannel openBufferedChannel(Path zipFile) throws IOException {
SeekableByteChannel channel = Files.newByteChannel(zipFile);
try {
return new BufferedDataInputSeekableChannel(channel, 8192, ByteOrder.LITTLE_ENDIAN);
} catch (Throwable e) {
IOUtils.closeQuietly(channel, e);
throw e;
}
}
public static ZipFileTree openZipTree(Path zipFile) throws IOException {
return new ZipFileTree(openZipFile(zipFile));
}
@@ -152,14 +137,14 @@ public final class CompressingUtils {
}
public static ZipArchiveReader openZipFile(Path zipFile, Charset charset) throws IOException {
return new ZipArchiveReader(openBufferedChannel(zipFile), charset);
return new ZipArchiveReader(zipFile, charset, true, true);
}
public static ZipArchiveReader openZipFileWithPossibleEncoding(Path zipFile, Charset possibleEncoding) throws IOException {
if (possibleEncoding == null)
possibleEncoding = StandardCharsets.UTF_8;
ZipArchiveReader zipReader = new ZipArchiveReader(openBufferedChannel(zipFile));
ZipArchiveReader zipReader = new ZipArchiveReader(zipFile, possibleEncoding, true, true);
Charset suitableEncoding;
try {
@@ -176,7 +161,7 @@ public final class CompressingUtils {
}
zipReader.close();
return new ZipArchiveReader(openBufferedChannel(zipFile), suitableEncoding);
return new ZipArchiveReader(zipFile, suitableEncoding, true, true);
}
public static final class Builder {
@@ -18,7 +18,7 @@
package org.jackhuang.hmcl.util.tree;
import kala.compress.archivers.ArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
@@ -44,7 +44,7 @@ public abstract class ArchiveFileTree<R, E extends ArchiveEntry> implements Clos
String name = namePath.toString();
if (name.endsWith(".jar") || name.endsWith(".zip")) {
return new ZipFileTree(new ZipArchiveReader(file));
return CompressingUtils.openZipTree(file);
} else if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
return TarFileTree.open(file);
} else {