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) } }