使用 Kala Compress 替代 commons-compress (#3481)

This commit is contained in:
Glavo
2025-04-08 17:36:36 +08:00
committed by GitHub
parent 99a031cea4
commit 62e965c6fe
17 changed files with 61 additions and 66 deletions

View File

@@ -18,7 +18,7 @@
package org.jackhuang.hmcl.game;
import com.google.gson.JsonParseException;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.mod.MismatchedModpackTypeException;
import org.jackhuang.hmcl.mod.Modpack;
@@ -64,7 +64,7 @@ public final class HMCLModpackProvider implements ModpackProvider {
}
@Override
public Modpack readManifest(ZipFile file, Path path, Charset encoding) throws IOException, JsonParseException {
public Modpack readManifest(ZipArchiveReader file, Path path, Charset encoding) throws IOException, JsonParseException {
String manifestJson = CompressingUtils.readTextZipEntry(file, "modpack.json");
Modpack manifest = JsonUtils.fromNonNullJson(manifestJson, HMCLModpack.class).setEncoding(encoding);
String gameJson = CompressingUtils.readTextZipEntry(file, "minecraft/pack.json");

View File

@@ -18,7 +18,7 @@
package org.jackhuang.hmcl.game;
import com.google.gson.JsonParseException;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.mod.*;
import org.jackhuang.hmcl.mod.curse.CurseModpackProvider;
import org.jackhuang.hmcl.mod.mcbbs.McbbsModpackManifest;
@@ -83,7 +83,7 @@ public final class ModpackHelper {
}
public static Modpack readModpackManifest(Path file, Charset charset) throws UnsupportedModpackException, ManuallyCreatedModpackException {
try (ZipFile zipFile = CompressingUtils.openZipFile(file, charset)) {
try (ZipArchiveReader zipFile = CompressingUtils.openZipFile(file, charset)) {
// Order for trying detecting manifest is necessary here.
// Do not change to iterating providers.
for (ModpackProvider provider : new ModpackProvider[]{

View File

@@ -17,7 +17,7 @@
*/
package org.jackhuang.hmcl.java;
import org.apache.commons.compress.archivers.ArchiveEntry;
import kala.compress.archivers.ArchiveEntry;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.DigestUtils;
import org.jackhuang.hmcl.util.Hex;

View File

@@ -22,7 +22,7 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.apache.commons.compress.utils.BoundedInputStream;
import kala.compress.utils.BoundedInputStream;
import org.jackhuang.hmcl.util.io.FileUtils;
import java.io.*;

View File

@@ -3,6 +3,10 @@ plugins {
}
dependencies {
val kalaCompressVersion = "1.27.1-1"
api("org.glavo.kala:kala-compress-archivers-zip:$kalaCompressVersion")
api("org.glavo.kala:kala-compress-archivers-tar:$kalaCompressVersion")
api("org.glavo:simple-png-javafx:0.3.0")
api("com.google.code.gson:gson:2.12.1")
api("com.moandjiezana.toml:toml4j:0.7.2")
@@ -11,7 +15,6 @@ dependencies {
api("org.jenkins-ci:constant-pool-scanner:1.2")
api("com.github.steveice10:opennbt:1.5")
api("org.nanohttpd:nanohttpd:2.3.1")
api("org.apache.commons:commons-compress:1.25.0")
api("org.jsoup:jsoup:1.18.3")
compileOnlyApi("org.jetbrains:annotations:26.0.1")

View File

@@ -17,7 +17,7 @@
*/
package org.jackhuang.hmcl.java;
import org.apache.commons.compress.archivers.ArchiveEntry;
import kala.compress.archivers.ArchiveEntry;
import org.jackhuang.hmcl.util.KeyValuePairProperties;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.platform.Architecture;

View File

@@ -18,7 +18,7 @@
package org.jackhuang.hmcl.mod;
import com.google.gson.JsonParseException;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.game.LaunchOptions;
import org.jackhuang.hmcl.task.Task;
@@ -44,7 +44,7 @@ public interface ModpackProvider {
* @throws JsonParseException if the manifest.json is missing or malformed.
* @return the manifest.
*/
Modpack readManifest(ZipFile zipFile, Path file, Charset encoding) throws IOException, JsonParseException;
Modpack readManifest(ZipArchiveReader zipFile, Path file, Charset encoding) throws IOException, JsonParseException;
default void injectLaunchOptions(String modpackConfigurationJson, LaunchOptions.Builder builder) {
}

View File

@@ -18,8 +18,8 @@
package org.jackhuang.hmcl.mod.curse;
import com.google.gson.JsonParseException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.mod.MismatchedModpackTypeException;
import org.jackhuang.hmcl.mod.Modpack;
@@ -57,7 +57,7 @@ public final class CurseModpackProvider implements ModpackProvider {
}
@Override
public Modpack readManifest(ZipFile zip, Path file, Charset encoding) throws IOException, JsonParseException {
public Modpack readManifest(ZipArchiveReader zip, Path file, Charset encoding) throws IOException, JsonParseException {
CurseManifest manifest = JsonUtils.fromNonNullJson(CompressingUtils.readTextZipEntry(zip, "manifest.json"), CurseManifest.class);
String description = "No description";
try {

View File

@@ -18,8 +18,8 @@
package org.jackhuang.hmcl.mod.mcbbs;
import com.google.gson.JsonParseException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.game.LaunchOptions;
import org.jackhuang.hmcl.mod.*;
@@ -70,7 +70,7 @@ public final class McbbsModpackProvider implements ModpackProvider {
}
@Override
public Modpack readManifest(ZipFile zip, Path file, Charset encoding) throws IOException, JsonParseException {
public Modpack readManifest(ZipArchiveReader zip, Path file, Charset encoding) throws IOException, JsonParseException {
ZipArchiveEntry mcbbsPackMeta = zip.getEntry("mcbbs.packmeta");
if (mcbbsPackMeta != null) {
return fromManifestFile(zip.getInputStream(mcbbsPackMeta), encoding);

View File

@@ -18,7 +18,7 @@
package org.jackhuang.hmcl.mod.modrinth;
import com.google.gson.JsonParseException;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.mod.MismatchedModpackTypeException;
import org.jackhuang.hmcl.mod.Modpack;
@@ -55,7 +55,7 @@ public final class ModrinthModpackProvider implements ModpackProvider {
}
@Override
public Modpack readManifest(ZipFile zip, Path file, Charset encoding) throws IOException, JsonParseException {
public Modpack readManifest(ZipArchiveReader zip, Path file, Charset encoding) throws IOException, JsonParseException {
ModrinthManifest manifest = JsonUtils.fromNonNullJson(CompressingUtils.readTextZipEntry(zip, "modrinth.index.json"), ModrinthManifest.class);
return new Modpack(manifest.getName(), "", manifest.getVersionId(), manifest.getGameVersion(), manifest.getSummary(), encoding, manifest) {
@Override

View File

@@ -18,8 +18,8 @@
package org.jackhuang.hmcl.mod.multimc;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.gson.JsonUtils;
@@ -55,7 +55,7 @@ public final class MultiMCManifest {
* @throws IOException if zip file is malformed
* @throws com.google.gson.JsonParseException if manifest is malformed.
*/
public static MultiMCManifest readMultiMCModpackManifest(ZipFile zipFile, String rootEntryName) throws IOException {
public static MultiMCManifest readMultiMCModpackManifest(ZipArchiveReader zipFile, String rootEntryName) throws IOException {
ZipArchiveEntry mmcPack = zipFile.getEntry(rootEntryName + "mmc-pack.json");
if (mmcPack == null)
return null;

View File

@@ -17,8 +17,8 @@
*/
package org.jackhuang.hmcl.mod.multimc;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.mod.MismatchedModpackTypeException;
import org.jackhuang.hmcl.mod.Modpack;
@@ -33,7 +33,6 @@ import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.stream.Stream;
public final class MultiMCModpackProvider implements ModpackProvider {
@@ -71,14 +70,12 @@ public final class MultiMCModpackProvider implements ModpackProvider {
}
}
private static String getRootEntryName(ZipFile file) throws IOException {
private static String getRootEntryName(ZipArchiveReader file) throws IOException {
final String instanceFileName = "instance.cfg";
if (file.getEntry(instanceFileName) != null) return "";
Enumeration<ZipArchiveEntry> entries = file.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
for (ZipArchiveEntry entry : file.getEntries()) {
String entryName = entry.getName();
int idx = entryName.indexOf('/');
@@ -92,7 +89,7 @@ public final class MultiMCModpackProvider implements ModpackProvider {
}
@Override
public Modpack readManifest(ZipFile modpackFile, Path modpackPath, Charset encoding) throws IOException {
public Modpack readManifest(ZipArchiveReader modpackFile, Path modpackPath, Charset encoding) throws IOException {
String rootEntryName = getRootEntryName(modpackFile);
MultiMCManifest manifest = MultiMCManifest.readMultiMCModpackManifest(modpackFile, rootEntryName);

View File

@@ -18,7 +18,7 @@
package org.jackhuang.hmcl.mod.server;
import com.google.gson.JsonParseException;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.mod.MismatchedModpackTypeException;
import org.jackhuang.hmcl.mod.Modpack;
@@ -55,7 +55,7 @@ public final class ServerModpackProvider implements ModpackProvider {
}
@Override
public Modpack readManifest(ZipFile zip, Path file, Charset encoding) throws IOException, JsonParseException {
public Modpack readManifest(ZipArchiveReader zip, Path file, Charset encoding) throws IOException, JsonParseException {
String json = CompressingUtils.readTextZipEntry(zip, "server-manifest.json");
ServerModpackManifest manifest = JsonUtils.fromNonNullJson(json, ServerModpackManifest.class);
return manifest.toModpack(encoding);

View File

@@ -17,8 +17,8 @@
*/
package org.jackhuang.hmcl.util.io;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
@@ -54,19 +54,16 @@ public final class CompressingUtils {
}
public static boolean testEncoding(Path zipFile, Charset encoding) throws IOException {
try (ZipFile zf = openZipFile(zipFile, encoding)) {
try (ZipArchiveReader zf = openZipFile(zipFile, encoding)) {
return testEncoding(zf, encoding);
}
}
public static boolean testEncoding(ZipFile zipFile, Charset encoding) throws IOException {
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
public static boolean testEncoding(ZipArchiveReader zipFile, Charset encoding) throws IOException {
CharsetDecoder cd = newCharsetDecoder(encoding);
CharBuffer cb = CharBuffer.allocate(32);
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
for (ZipArchiveEntry entry : zipFile.getEntries()) {
if (entry.getGeneralPurposeBit().usesUTF8ForNames()) continue;
cd.reset();
@@ -88,12 +85,12 @@ public final class CompressingUtils {
}
public static Charset findSuitableEncoding(Path zipFile) throws IOException {
try (ZipFile zf = openZipFile(zipFile, StandardCharsets.UTF_8)) {
try (ZipArchiveReader zf = openZipFile(zipFile, StandardCharsets.UTF_8)) {
return findSuitableEncoding(zf);
}
}
public static Charset findSuitableEncoding(ZipFile zipFile) throws IOException {
public static Charset findSuitableEncoding(ZipArchiveReader zipFile) throws IOException {
if (testEncoding(zipFile, StandardCharsets.UTF_8)) return StandardCharsets.UTF_8;
if (OperatingSystem.NATIVE_CHARSET != StandardCharsets.UTF_8 && testEncoding(zipFile, OperatingSystem.NATIVE_CHARSET))
return OperatingSystem.NATIVE_CHARSET;
@@ -133,12 +130,12 @@ public final class CompressingUtils {
throw new IOException("Cannot find suitable encoding for the zip.");
}
public static ZipFile openZipFile(Path zipFile) throws IOException {
return new ZipFile(Files.newByteChannel(zipFile));
public static ZipArchiveReader openZipFile(Path zipFile) throws IOException {
return new ZipArchiveReader(Files.newByteChannel(zipFile));
}
public static ZipFile openZipFile(Path zipFile, Charset charset) throws IOException {
return new ZipFile(Files.newByteChannel(zipFile), charset.name());
public static ZipArchiveReader openZipFile(Path zipFile, Charset charset) throws IOException {
return new ZipArchiveReader(zipFile, charset);
}
public static final class Builder {
@@ -234,7 +231,7 @@ public final class CompressingUtils {
* @return the plain text content of given file.
*/
public static String readTextZipEntry(File zipFile, String name) throws IOException {
try (ZipFile s = new ZipFile(zipFile)) {
try (ZipArchiveReader s = new ZipArchiveReader(zipFile.toPath())) {
return readTextZipEntry(s, name);
}
}
@@ -247,7 +244,7 @@ public final class CompressingUtils {
* @throws IOException if the file is not a valid zip file.
* @return the plain text content of given file.
*/
public static String readTextZipEntry(ZipFile zipFile, String name) throws IOException {
public static String readTextZipEntry(ZipArchiveReader zipFile, String name) throws IOException {
return IOUtils.readFullyAsString(zipFile.getInputStream(zipFile.getEntry(name)));
}
@@ -260,7 +257,7 @@ public final class CompressingUtils {
* @return the plain text content of given file.
*/
public static String readTextZipEntry(Path zipFile, String name, Charset encoding) throws IOException {
try (ZipFile s = openZipFile(zipFile, encoding)) {
try (ZipArchiveReader s = openZipFile(zipFile, encoding)) {
return IOUtils.readFullyAsString(s.getInputStream(s.getEntry(name)));
}
}

View File

@@ -17,8 +17,8 @@
*/
package org.jackhuang.hmcl.util.tree;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.ArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import java.io.Closeable;
import java.io.IOException;
@@ -39,7 +39,7 @@ public abstract class ArchiveFileTree<F, E extends ArchiveEntry> implements Clos
String name = namePath.toString();
if (name.endsWith(".jar") || name.endsWith(".zip")) {
return new ZipFileTree(new ZipFile(file));
return new ZipFileTree(new ZipArchiveReader(file));
} else if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
return TarFileTree.open(file);
} else {

View File

@@ -18,8 +18,8 @@
package org.jackhuang.hmcl.util.tree;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarFile;
import kala.compress.archivers.tar.TarArchiveEntry;
import kala.compress.archivers.tar.TarArchiveReader;
import org.jackhuang.hmcl.util.io.IOUtils;
import java.io.*;
@@ -31,19 +31,19 @@ import java.util.zip.GZIPInputStream;
/**
* @author Glavo
*/
public final class TarFileTree extends ArchiveFileTree<TarFile, TarArchiveEntry> {
public final class TarFileTree extends ArchiveFileTree<TarArchiveReader, TarArchiveEntry> {
public static TarFileTree open(Path file) throws IOException {
String fileName = file.getFileName().toString();
if (fileName.endsWith(".tar.gz") || fileName.endsWith(".tgz")) {
Path tempFile = Files.createTempFile("hmcl-", ".tar");
TarFile tarFile;
TarArchiveReader tarFile;
try (GZIPInputStream input = new GZIPInputStream(Files.newInputStream(file));
OutputStream output = Files.newOutputStream(tempFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
) {
IOUtils.copyTo(input, output);
tarFile = new TarFile(tempFile.toFile());
tarFile = new TarArchiveReader(tempFile);
} catch (Throwable e) {
try {
Files.deleteIfExists(tempFile);
@@ -55,14 +55,14 @@ public final class TarFileTree extends ArchiveFileTree<TarFile, TarArchiveEntry>
return new TarFileTree(tarFile, tempFile);
} else {
return new TarFileTree(new TarFile(file), null);
return new TarFileTree(new TarArchiveReader(file), null);
}
}
private final Path tempFile;
private final Thread shutdownHook;
public TarFileTree(TarFile file, Path tempFile) throws IOException {
public TarFileTree(TarArchiveReader file, Path tempFile) throws IOException {
super(file);
this.tempFile = tempFile;
try {

View File

@@ -17,23 +17,21 @@
*/
package org.jackhuang.hmcl.util.tree;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import kala.compress.archivers.zip.ZipArchiveEntry;
import kala.compress.archivers.zip.ZipArchiveReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
/**
* @author Glavo
*/
public final class ZipFileTree extends ArchiveFileTree<ZipFile, ZipArchiveEntry> {
public ZipFileTree(ZipFile file) throws IOException {
public final class ZipFileTree extends ArchiveFileTree<ZipArchiveReader, ZipArchiveEntry> {
public ZipFileTree(ZipArchiveReader file) throws IOException {
super(file);
try {
Enumeration<ZipArchiveEntry> entries = file.getEntries();
while (entries.hasMoreElements()) {
addEntry(entries.nextElement());
for (ZipArchiveEntry zipArchiveEntry : file.getEntries()) {
addEntry(zipArchiveEntry);
}
} catch (Throwable e) {
try {