Apache common-compress

This commit is contained in:
huangyuhui
2017-08-25 10:12:56 +08:00
parent cc2c186e7f
commit e70226afba
5 changed files with 23 additions and 30 deletions

View File

@@ -17,6 +17,7 @@
*/ */
package org.jackhuang.hmcl.download.forge 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.DefaultDependencyManager
import org.jackhuang.hmcl.download.RemoteVersion import org.jackhuang.hmcl.download.RemoteVersion
import org.jackhuang.hmcl.download.game.GameLibrariesTask import org.jackhuang.hmcl.download.game.GameLibrariesTask
@@ -30,7 +31,6 @@ import org.jackhuang.hmcl.task.then
import org.jackhuang.hmcl.util.* import org.jackhuang.hmcl.util.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.util.zip.ZipFile
class ForgeInstallTask(private val dependencyManager: DefaultDependencyManager, class ForgeInstallTask(private val dependencyManager: DefaultDependencyManager,
private val gameVersion: String, private val gameVersion: String,

View File

@@ -19,12 +19,12 @@ package org.jackhuang.hmcl.mod
import com.google.gson.JsonParseException import com.google.gson.JsonParseException
import com.google.gson.annotations.SerializedName 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.GSON
import org.jackhuang.hmcl.util.parseParams import org.jackhuang.hmcl.util.parseParams
import org.jackhuang.hmcl.util.readFullyAsString import org.jackhuang.hmcl.util.readFullyAsString
import org.jackhuang.hmcl.util.typeOf import org.jackhuang.hmcl.util.typeOf
import java.io.File import java.io.File
import java.util.zip.ZipFile
class ForgeModMetadata @JvmOverloads internal constructor( class ForgeModMetadata @JvmOverloads internal constructor(
@SerializedName("modid") @SerializedName("modid")

View File

@@ -18,17 +18,16 @@
package org.jackhuang.hmcl.mod package org.jackhuang.hmcl.mod
import com.google.gson.annotations.SerializedName 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.DefaultDependencyManager
import org.jackhuang.hmcl.download.game.VersionJSONSaveTask import org.jackhuang.hmcl.download.game.VersionJSONSaveTask
import org.jackhuang.hmcl.game.Library import org.jackhuang.hmcl.game.Library
import org.jackhuang.hmcl.game.Version
import org.jackhuang.hmcl.task.Task import org.jackhuang.hmcl.task.Task
import org.jackhuang.hmcl.util.* import org.jackhuang.hmcl.util.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.util.* import java.util.*
import java.util.zip.ZipFile
class InstancePatch @JvmOverloads constructor( class InstancePatch @JvmOverloads constructor(
val name: String = "", val name: String = "",
@@ -221,7 +220,7 @@ class MMCModpackInstallTask(private val dependencyManager: DefaultDependencyMana
unzipSubDirectory(zipFile, run, "minecraft/", false) unzipSubDirectory(zipFile, run, "minecraft/", false)
ZipFile(zipFile).use { zip -> 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. // ensure that this entry is in folder 'patches' and is a json file.
if (!entry.isDirectory && entry.name.startsWith("patches/") && entry.name.endsWith(".json")) { if (!entry.isDirectory && entry.name.startsWith("patches/") && entry.name.endsWith(".json")) {
val patch = GSON.fromJson<InstancePatch>(zip.getInputStream(entry).readFullyAsString())!! val patch = GSON.fromJson<InstancePatch>(zip.getInputStream(entry).readFullyAsString())!!

View File

@@ -17,12 +17,12 @@
*/ */
package org.jackhuang.hmcl.util 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.File
import java.io.IOException 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. * Compress the given directory to a zip file.
@@ -35,7 +35,7 @@ import java.util.zip.ZipOutputStream
@JvmOverloads @JvmOverloads
@Throws(IOException::class) @Throws(IOException::class)
fun zip(src: File, destZip: File, pathNameCallback: ((String, Boolean) -> String?)? = null) { fun zip(src: File, destZip: File, pathNameCallback: ((String, Boolean) -> String?)? = null) {
ZipOutputStream(destZip.outputStream()).use { zos -> ZipArchiveOutputStream(destZip.outputStream()).use { zos ->
val basePath: String val basePath: String
if (src.isDirectory) if (src.isDirectory)
basePath = src.path basePath = src.path
@@ -43,7 +43,7 @@ fun zip(src: File, destZip: File, pathNameCallback: ((String, Boolean) -> String
//直接压缩单个文件时,取父目录 //直接压缩单个文件时,取父目录
basePath = src.parent basePath = src.parent
zipFile(src, basePath, zos, pathNameCallback) 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) @Throws(IOException::class)
private fun zipFile(src: File, private fun zipFile(src: File,
basePath: String, basePath: String,
zos: ZipOutputStream, zos: ZipArchiveOutputStream,
pathNameCallback: ((String, Boolean) -> String?)?) { pathNameCallback: ((String, Boolean) -> String?)?) {
val files: Array<File> val files: Array<File>
if (src.isDirectory) if (src.isDirectory)
@@ -75,7 +75,7 @@ private fun zipFile(src: File,
pathName = pathNameCallback.invoke(pathName, true) pathName = pathNameCallback.invoke(pathName, true)
if (pathName == null) if (pathName == null)
continue continue
zos.putNextEntry(ZipEntry(pathName)) zos.putArchiveEntry(ZipArchiveEntry(pathName))
zipFile(file, basePath, zos, pathNameCallback) zipFile(file, basePath, zos, pathNameCallback)
} else { } else {
pathName = file.path.substring(basePath.length + 1) pathName = file.path.substring(basePath.length + 1)
@@ -84,7 +84,7 @@ private fun zipFile(src: File,
if (pathName == null) if (pathName == null)
continue continue
file.inputStream().use { inputStream -> file.inputStream().use { inputStream ->
zos.putNextEntry(ZipEntry(pathName)) zos.putArchiveEntry(ZipArchiveEntry(pathName))
inputStream.copyTo(zos, buf) 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) { fun unzip(zip: File, dest: File, callback: ((String) -> Boolean)? = null, ignoreExistsFile: Boolean = true) {
val buf = ByteArray(1024) val buf = ByteArray(1024)
dest.mkdirs() dest.mkdirs()
ZipInputStream(zip.inputStream()).use { zipFile -> ZipArchiveInputStream(zip.inputStream()).use { zipFile ->
if (zip.exists()) { if (zip.exists()) {
var gbkPath: String
var strtemp: String
val strPath = dest.absolutePath val strPath = dest.absolutePath
var zipEnt: ZipEntry?
while (true) { while (true) {
zipEnt = zipFile.nextEntry val zipEnt = zipFile.nextEntry ?: break
if (zipEnt == null) var strtemp: String
break var gbkPath = zipEnt.name
gbkPath = zipEnt.name
if (callback != null) if (callback != null)
if (!callback.invoke(gbkPath)) if (!callback.invoke(gbkPath))
continue continue
@@ -126,7 +122,7 @@ fun unzip(zip: File, dest: File, callback: ((String) -> Boolean)? = null, ignore
strtemp = strPath + File.separator + gbkPath strtemp = strPath + File.separator + gbkPath
//建目录 //建目录
val strsubdir = 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)) { if (strsubdir.substring(i, i + 1).equals("/", ignoreCase = true)) {
val temp = strPath + File.separator + strsubdir.substring(0, i) val temp = strPath + File.separator + strsubdir.substring(0, i)
val subdir = File(temp) 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) { fun unzipSubDirectory(zip: File, dest: File, subDirectory: String, ignoreExistentFile: Boolean = true) {
val buf = ByteArray(1024) val buf = ByteArray(1024)
dest.mkdirs() dest.mkdirs()
ZipInputStream(zip.inputStream()).use { zipFile -> ZipArchiveInputStream(zip.inputStream()).use { zipFile ->
if (zip.exists()) { if (zip.exists()) {
var gbkPath: String
var strtemp: String
val strPath = dest.absolutePath val strPath = dest.absolutePath
var zipEnt: ZipEntry?
while (true) { while (true) {
zipEnt = zipFile.nextEntry val zipEnt = zipFile.nextEntry ?: break
if (zipEnt == null) var strtemp: String
break var gbkPath: String
gbkPath = zipEnt.name gbkPath = zipEnt.name
if (!gbkPath.startsWith(subDirectory)) if (!gbkPath.startsWith(subDirectory))
continue continue

View File

@@ -60,6 +60,7 @@ allprojects {
dependencies { dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile "com.google.code.gson:gson:2.8.1" 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' testCompile group: 'junit', name: 'junit', version: '4.12'
} }