优化代码

pull/768/head
gedoor 4 years ago
parent 1adce9267b
commit 83fc64d806
  1. 40
      app/src/main/java/io/legado/app/utils/ZipUtils.kt

@ -1,13 +1,15 @@
package io.legado.app.utils package io.legado.app.utils
import android.util.Log import android.util.Log
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import java.io.* import java.io.*
import java.util.* import java.util.*
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipFile import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream import java.util.zip.ZipOutputStream
@Suppress("unused") @Suppress("unused", "BlockingMethodInNonBlockingContext")
object ZipUtils { object ZipUtils {
/** /**
@ -38,20 +40,14 @@ object ZipUtils {
srcFilePaths: Collection<String>?, srcFilePaths: Collection<String>?,
zipFilePath: String?, zipFilePath: String?,
comment: String? comment: String?
): Boolean { ): Boolean = withContext(IO) {
if (srcFilePaths == null || zipFilePath == null) return false if (srcFilePaths == null || zipFilePath == null) return@withContext false
var zos: ZipOutputStream? = null ZipOutputStream(FileOutputStream(zipFilePath)).use {
try {
zos = ZipOutputStream(FileOutputStream(zipFilePath))
for (srcFile in srcFilePaths) { for (srcFile in srcFilePaths) {
if (!zipFile(getFileByPath(srcFile)!!, "", zos, comment)) return false if (!zipFile(getFileByPath(srcFile)!!, "", it, comment))
} return@withContext false
return true
} finally {
zos?.let {
zos.finish()
zos.close()
} }
return@withContext true
} }
} }
@ -72,18 +68,11 @@ object ZipUtils {
comment: String? = null comment: String? = null
): Boolean { ): Boolean {
if (srcFiles == null || zipFile == null) return false if (srcFiles == null || zipFile == null) return false
var zos: ZipOutputStream? = null ZipOutputStream(FileOutputStream(zipFile)).use {
try {
zos = ZipOutputStream(FileOutputStream(zipFile))
for (srcFile in srcFiles) { for (srcFile in srcFiles) {
if (!zipFile(srcFile, "", zos, comment)) return false if (!zipFile(srcFile, "", it, comment)) return false
} }
return true return true
} finally {
zos?.let {
zos.finish()
zos.close()
}
} }
} }
@ -139,12 +128,7 @@ object ZipUtils {
): Boolean { ): Boolean {
if (srcFile == null || zipFile == null) return false if (srcFile == null || zipFile == null) return false
ZipOutputStream(FileOutputStream(zipFile)).use { zos -> ZipOutputStream(FileOutputStream(zipFile)).use { zos ->
return zipFile( return zipFile(srcFile, "", zos, comment)
srcFile,
"",
zos,
comment
)
} }
} }

Loading…
Cancel
Save