From e70226afba7d8c6521bd73e556751953d9cd2dc1 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Fri, 25 Aug 2017 10:12:56 +0800 Subject: [PATCH] Apache common-compress --- .../hmcl/download/forge/ForgeInstallTask.kt | 2 +- .../jackhuang/hmcl/mod/ForgeModMetadata.kt | 2 +- .../org/jackhuang/hmcl/mod/MultiMCModpack.kt | 5 +-- .../org/jackhuang/hmcl/util/Compressors.kt | 43 ++++++++----------- build.gradle | 1 + 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/download/forge/ForgeInstallTask.kt b/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/download/forge/ForgeInstallTask.kt index 1249a8cbc..6c34d0daf 100644 --- a/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/download/forge/ForgeInstallTask.kt +++ b/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/download/forge/ForgeInstallTask.kt @@ -17,6 +17,7 @@ */ package org.jackhuang.hmcl.download.forge +import org.apache.commons.compress.archivers.zip.ZipFile import org.jackhuang.hmcl.download.DefaultDependencyManager import org.jackhuang.hmcl.download.RemoteVersion import org.jackhuang.hmcl.download.game.GameLibrariesTask @@ -30,7 +31,6 @@ import org.jackhuang.hmcl.task.then import org.jackhuang.hmcl.util.* import java.io.File import java.io.IOException -import java.util.zip.ZipFile class ForgeInstallTask(private val dependencyManager: DefaultDependencyManager, private val gameVersion: String, diff --git a/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/mod/ForgeModMetadata.kt b/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/mod/ForgeModMetadata.kt index 8c93066a0..14bd42ed0 100644 --- a/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/mod/ForgeModMetadata.kt +++ b/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/mod/ForgeModMetadata.kt @@ -19,12 +19,12 @@ package org.jackhuang.hmcl.mod import com.google.gson.JsonParseException import com.google.gson.annotations.SerializedName +import org.apache.commons.compress.archivers.zip.ZipFile import org.jackhuang.hmcl.util.GSON import org.jackhuang.hmcl.util.parseParams import org.jackhuang.hmcl.util.readFullyAsString import org.jackhuang.hmcl.util.typeOf import java.io.File -import java.util.zip.ZipFile class ForgeModMetadata @JvmOverloads internal constructor( @SerializedName("modid") diff --git a/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/mod/MultiMCModpack.kt b/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/mod/MultiMCModpack.kt index fddb44acb..599888e69 100644 --- a/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/mod/MultiMCModpack.kt +++ b/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/mod/MultiMCModpack.kt @@ -18,17 +18,16 @@ package org.jackhuang.hmcl.mod import com.google.gson.annotations.SerializedName +import org.apache.commons.compress.archivers.zip.ZipFile import org.jackhuang.hmcl.download.DefaultDependencyManager import org.jackhuang.hmcl.download.game.VersionJSONSaveTask import org.jackhuang.hmcl.game.Library -import org.jackhuang.hmcl.game.Version import org.jackhuang.hmcl.task.Task import org.jackhuang.hmcl.util.* import java.io.File import java.io.IOException import java.io.InputStream import java.util.* -import java.util.zip.ZipFile class InstancePatch @JvmOverloads constructor( val name: String = "", @@ -221,7 +220,7 @@ class MMCModpackInstallTask(private val dependencyManager: DefaultDependencyMana unzipSubDirectory(zipFile, run, "minecraft/", false) ZipFile(zipFile).use { zip -> - for (entry in zip.entries()) { + for (entry in zip.entries) { // ensure that this entry is in folder 'patches' and is a json file. if (!entry.isDirectory && entry.name.startsWith("patches/") && entry.name.endsWith(".json")) { val patch = GSON.fromJson(zip.getInputStream(entry).readFullyAsString())!! diff --git a/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/util/Compressors.kt b/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/util/Compressors.kt index c12508635..88b70cf62 100644 --- a/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/util/Compressors.kt +++ b/HMCLCore/src/main/kotlin/org/jackhuang/hmcl/util/Compressors.kt @@ -17,12 +17,12 @@ */ package org.jackhuang.hmcl.util +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry +import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream +import org.apache.commons.compress.archivers.zip.ZipFile import java.io.File import java.io.IOException -import java.util.zip.ZipEntry -import java.util.zip.ZipFile -import java.util.zip.ZipInputStream -import java.util.zip.ZipOutputStream /** * Compress the given directory to a zip file. @@ -35,7 +35,7 @@ import java.util.zip.ZipOutputStream @JvmOverloads @Throws(IOException::class) fun zip(src: File, destZip: File, pathNameCallback: ((String, Boolean) -> String?)? = null) { - ZipOutputStream(destZip.outputStream()).use { zos -> + ZipArchiveOutputStream(destZip.outputStream()).use { zos -> val basePath: String if (src.isDirectory) basePath = src.path @@ -43,7 +43,7 @@ fun zip(src: File, destZip: File, pathNameCallback: ((String, Boolean) -> String //直接压缩单个文件时,取父目录 basePath = src.parent zipFile(src, basePath, zos, pathNameCallback) - zos.closeEntry() + zos.closeArchiveEntry() } } @@ -58,7 +58,7 @@ fun zip(src: File, destZip: File, pathNameCallback: ((String, Boolean) -> String @Throws(IOException::class) private fun zipFile(src: File, basePath: String, - zos: ZipOutputStream, + zos: ZipArchiveOutputStream, pathNameCallback: ((String, Boolean) -> String?)?) { val files: Array if (src.isDirectory) @@ -75,7 +75,7 @@ private fun zipFile(src: File, pathName = pathNameCallback.invoke(pathName, true) if (pathName == null) continue - zos.putNextEntry(ZipEntry(pathName)) + zos.putArchiveEntry(ZipArchiveEntry(pathName)) zipFile(file, basePath, zos, pathNameCallback) } else { pathName = file.path.substring(basePath.length + 1) @@ -84,7 +84,7 @@ private fun zipFile(src: File, if (pathName == null) continue file.inputStream().use { inputStream -> - zos.putNextEntry(ZipEntry(pathName)) + zos.putArchiveEntry(ZipArchiveEntry(pathName)) inputStream.copyTo(zos, buf) } } @@ -102,17 +102,13 @@ private fun zipFile(src: File, fun unzip(zip: File, dest: File, callback: ((String) -> Boolean)? = null, ignoreExistsFile: Boolean = true) { val buf = ByteArray(1024) dest.mkdirs() - ZipInputStream(zip.inputStream()).use { zipFile -> + ZipArchiveInputStream(zip.inputStream()).use { zipFile -> if (zip.exists()) { - var gbkPath: String - var strtemp: String val strPath = dest.absolutePath - var zipEnt: ZipEntry? while (true) { - zipEnt = zipFile.nextEntry - if (zipEnt == null) - break - gbkPath = zipEnt.name + val zipEnt = zipFile.nextEntry ?: break + var strtemp: String + var gbkPath = zipEnt.name if (callback != null) if (!callback.invoke(gbkPath)) continue @@ -126,7 +122,7 @@ fun unzip(zip: File, dest: File, callback: ((String) -> Boolean)? = null, ignore strtemp = strPath + File.separator + gbkPath //建目录 val strsubdir = gbkPath - for (i in 0..strsubdir.length - 1) + for (i in 0 until strsubdir.length) if (strsubdir.substring(i, i + 1).equals("/", ignoreCase = true)) { val temp = strPath + File.separator + strsubdir.substring(0, i) val subdir = File(temp) @@ -157,16 +153,13 @@ fun unzip(zip: File, dest: File, callback: ((String) -> Boolean)? = null, ignore fun unzipSubDirectory(zip: File, dest: File, subDirectory: String, ignoreExistentFile: Boolean = true) { val buf = ByteArray(1024) dest.mkdirs() - ZipInputStream(zip.inputStream()).use { zipFile -> + ZipArchiveInputStream(zip.inputStream()).use { zipFile -> if (zip.exists()) { - var gbkPath: String - var strtemp: String val strPath = dest.absolutePath - var zipEnt: ZipEntry? while (true) { - zipEnt = zipFile.nextEntry - if (zipEnt == null) - break + val zipEnt = zipFile.nextEntry ?: break + var strtemp: String + var gbkPath: String gbkPath = zipEnt.name if (!gbkPath.startsWith(subDirectory)) continue diff --git a/build.gradle b/build.gradle index efe04fcba..cdaea85d1 100644 --- a/build.gradle +++ b/build.gradle @@ -60,6 +60,7 @@ allprojects { dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" compile "com.google.code.gson:gson:2.8.1" + compile "org.apache.commons:commons-compress:1.8.1" testCompile group: 'junit', name: 'junit', version: '4.12' }