Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 Kala Compress 替代 commons-compress #3481

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +64,7 @@ public Task<?> createUpdateTask(DefaultDependencyManager dependencyManager, Stri
}

@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");
Expand Down
4 changes: 2 additions & 2 deletions HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +83,7 @@ public static boolean isFileModpackByExtension(File file) {
}

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[]{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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.*;
Expand Down
5 changes: 4 additions & 1 deletion HMCLCore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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.11.0")
api("com.moandjiezana.toml:toml4j:0.7.2")
Expand All @@ -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.1")
compileOnlyApi("org.jetbrains:annotations:24.1.0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,7 +57,7 @@ public Task<?> createUpdateTask(DefaultDependencyManager dependencyManager, Stri
}

@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -70,7 +70,7 @@ private static Modpack fromManifestFile(InputStream json, Charset encoding) thro
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,7 +55,7 @@ public Task<?> createUpdateTask(DefaultDependencyManager dependencyManager, Stri
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -55,7 +55,7 @@ public List<MultiMCManifestComponent> getComponents() {
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +33,6 @@
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 {
Expand Down Expand Up @@ -71,14 +70,12 @@ public static Path getRootPath(Path root) throws IOException {
}
}

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('/');
Expand All @@ -92,7 +89,7 @@ private static String getRootEntryName(ZipFile file) throws IOException {
}

@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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,7 +55,7 @@ public Task<?> createUpdateTask(DefaultDependencyManager dependencyManager, Stri
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -54,19 +54,16 @@ private static CharsetDecoder newCharsetDecoder(Charset charset) {
}

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();
Expand All @@ -88,12 +85,12 @@ public static boolean testEncoding(ZipFile zipFile, Charset encoding) throws IOE
}

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;
Expand Down Expand Up @@ -133,12 +130,12 @@ public static Charset findSuitableEncoding(ZipFile zipFile) throws IOException {
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 {
Expand Down Expand Up @@ -234,7 +231,7 @@ public static FileSystem createZipFileSystem(Path zipFile, boolean create, boole
* @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);
}
}
Expand All @@ -247,7 +244,7 @@ public static String readTextZipEntry(File zipFile, String name) throws IOExcept
* @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)));
}

Expand All @@ -260,7 +257,7 @@ public static String readTextZipEntry(ZipFile zipFile, String name) throws IOExc
* @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)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Loading