From 83fc64d806d943d79d4496e2e7b441f36f4cb60d Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 27 Dec 2020 23:06:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/utils/ZipUtils.kt | 40 ++++++------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/ZipUtils.kt b/app/src/main/java/io/legado/app/utils/ZipUtils.kt index 73311729d..b839066af 100644 --- a/app/src/main/java/io/legado/app/utils/ZipUtils.kt +++ b/app/src/main/java/io/legado/app/utils/ZipUtils.kt @@ -1,13 +1,15 @@ package io.legado.app.utils import android.util.Log +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.withContext import java.io.* import java.util.* import java.util.zip.ZipEntry import java.util.zip.ZipFile import java.util.zip.ZipOutputStream -@Suppress("unused") +@Suppress("unused", "BlockingMethodInNonBlockingContext") object ZipUtils { /** @@ -38,20 +40,14 @@ object ZipUtils { srcFilePaths: Collection?, zipFilePath: String?, comment: String? - ): Boolean { - if (srcFilePaths == null || zipFilePath == null) return false - var zos: ZipOutputStream? = null - try { - zos = ZipOutputStream(FileOutputStream(zipFilePath)) + ): Boolean = withContext(IO) { + if (srcFilePaths == null || zipFilePath == null) return@withContext false + ZipOutputStream(FileOutputStream(zipFilePath)).use { for (srcFile in srcFilePaths) { - if (!zipFile(getFileByPath(srcFile)!!, "", zos, comment)) return false - } - return true - } finally { - zos?.let { - zos.finish() - zos.close() + if (!zipFile(getFileByPath(srcFile)!!, "", it, comment)) + return@withContext false } + return@withContext true } } @@ -72,18 +68,11 @@ object ZipUtils { comment: String? = null ): Boolean { if (srcFiles == null || zipFile == null) return false - var zos: ZipOutputStream? = null - try { - zos = ZipOutputStream(FileOutputStream(zipFile)) + ZipOutputStream(FileOutputStream(zipFile)).use { for (srcFile in srcFiles) { - if (!zipFile(srcFile, "", zos, comment)) return false + if (!zipFile(srcFile, "", it, comment)) return false } return true - } finally { - zos?.let { - zos.finish() - zos.close() - } } } @@ -139,12 +128,7 @@ object ZipUtils { ): Boolean { if (srcFile == null || zipFile == null) return false ZipOutputStream(FileOutputStream(zipFile)).use { zos -> - return zipFile( - srcFile, - "", - zos, - comment - ) + return zipFile(srcFile, "", zos, comment) } }