From c378af0541243ddd4a27a938829fa6501929118d Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 20 Sep 2023 10:55:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 6 +- .../java/io/legado/app/constant/PreferKey.kt | 1 - .../io/legado/app/help/config/AppConfig.kt | 3 - .../app/ui/book/info/BookInfoViewModel.kt | 10 +- .../java/io/legado/app/utils/ArchiveUtils.kt | 68 +---------- .../app/utils/compress/LibArchiveUtils.kt | 108 +++++++++--------- app/src/main/res/xml/pref_config_other.xml | 6 - 7 files changed, 63 insertions(+), 139 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 54ccea8e4..c7657446b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -168,15 +168,15 @@ dependencies { implementation('com.github.android:renderscript-intrinsics-replacement-toolkit:b6363490c3') //androidX - implementation('androidx.core:core-ktx:1.10.1') + implementation('androidx.core:core-ktx:1.12.0') implementation('androidx.appcompat:appcompat:1.6.1') implementation('androidx.activity:activity-ktx:1.7.2') implementation('androidx.fragment:fragment-ktx:1.6.1') - implementation('androidx.preference:preference-ktx:1.2.0') + implementation('androidx.preference:preference-ktx:1.2.1') implementation('androidx.constraintlayout:constraintlayout:2.1.4') implementation('androidx.swiperefreshlayout:swiperefreshlayout:1.1.0') implementation('androidx.viewpager2:viewpager2:1.0.0') - implementation('androidx.webkit:webkit:1.7.0') + implementation('androidx.webkit:webkit:1.8.0') //google implementation('com.google.android.material:material:1.9.0') diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index e16ac6879..9a215a0f3 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -89,7 +89,6 @@ object PreferKey { const val syncBookProgress = "syncBookProgress" const val cronet = "Cronet" const val antiAlias = "antiAlias" - const val useLibArchive = "useLibArchive" const val bitmapCacheSize = "bitmapCacheSize" const val preDownloadNum = "preDownloadNum" const val autoRefresh = "auto_refresh" diff --git a/app/src/main/java/io/legado/app/help/config/AppConfig.kt b/app/src/main/java/io/legado/app/help/config/AppConfig.kt index 10c98ef02..4cd3dd4b5 100644 --- a/app/src/main/java/io/legado/app/help/config/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/AppConfig.kt @@ -25,7 +25,6 @@ import splitties.init.appCtx object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { val isCronet = appCtx.getPrefBoolean(PreferKey.cronet) var useAntiAlias = appCtx.getPrefBoolean(PreferKey.antiAlias) - var useLibArchive = appCtx.getPrefBoolean(PreferKey.useLibArchive) var userAgent: String = getPrefUserAgent() var isEInkMode = appCtx.getPrefString(PreferKey.themeMode) == "3" var clickActionTL = appCtx.getPrefInt(PreferKey.clickActionTL, 2) @@ -78,8 +77,6 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { PreferKey.antiAlias -> useAntiAlias = appCtx.getPrefBoolean(PreferKey.antiAlias) - PreferKey.useLibArchive -> useLibArchive = - appCtx.getPrefBoolean(PreferKey.useLibArchive) } } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index cb92a8332..962acd46d 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -5,7 +5,6 @@ import android.content.Intent import android.net.Uri import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope -import com.github.junrar.exception.UnsupportedRarV5Exception import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppLog @@ -299,13 +298,8 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { AppPattern.bookFileRegex.matches(it) } }.onError { - when (it) { - is UnsupportedRarV5Exception -> context.toastOnUi("暂不支持 rar v5 解压") - else -> { - AppLog.put("getArchiveEntriesName Error:\n${it.localizedMessage}", it) - context.toastOnUi("getArchiveEntriesName Error:\n${it.localizedMessage}") - } - } + AppLog.put("getArchiveEntriesName Error:\n${it.localizedMessage}", it) + context.toastOnUi("getArchiveEntriesName Error:\n${it.localizedMessage}") }.onSuccess { onSuccess.invoke(it) } diff --git a/app/src/main/java/io/legado/app/utils/ArchiveUtils.kt b/app/src/main/java/io/legado/app/utils/ArchiveUtils.kt index af686aa78..516725174 100644 --- a/app/src/main/java/io/legado/app/utils/ArchiveUtils.kt +++ b/app/src/main/java/io/legado/app/utils/ArchiveUtils.kt @@ -3,11 +3,7 @@ package io.legado.app.utils import android.net.Uri import androidx.documentfile.provider.DocumentFile import io.legado.app.constant.AppPattern.archiveFileRegex -import io.legado.app.help.config.AppConfig import io.legado.app.utils.compress.LibArchiveUtils -import io.legado.app.utils.compress.RarUtils -import io.legado.app.utils.compress.SevenZipUtils -import io.legado.app.utils.compress.ZipUtils import splitties.init.appCtx import java.io.File @@ -65,49 +61,18 @@ object ArchiveUtils { val workPathFileDoc = getCacheFolderFileDoc(name, path) val workPath = workPathFileDoc.toString() - return if (AppConfig.useLibArchive) { - archiveFileDoc.openReadPfd().getOrThrow().use { - LibArchiveUtils.unArchive(it, File(workPath), filter) - } - } else { - archiveFileDoc.openInputStream().getOrThrow().use { - when { - name.endsWith(".zip", ignoreCase = true) -> ZipUtils.unZipToPath( - it, - workPath, - filter - ) - - name.endsWith(".rar", ignoreCase = true) -> RarUtils.unRarToPath( - it, - workPath, - filter - ) - - name.endsWith(".7z", ignoreCase = true) -> SevenZipUtils.un7zToPath( - it, - workPath, - filter - ) - - else -> throw IllegalArgumentException("Unexpected archive format") - } - } + return archiveFileDoc.openReadPfd().getOrThrow().use { + LibArchiveUtils.unArchive(it, File(workPath), filter) } - } /* 遍历目录获取文件名 */ fun getArchiveFilesName(fileUri: Uri, filter: ((String) -> Boolean)? = null): List = - if (AppConfig.useLibArchive) { - getLibArchiveFilesName(FileDoc.fromUri(fileUri, false), filter) - } else { - getArchiveFilesName(FileDoc.fromUri(fileUri, false), filter) - } + getArchiveFilesName(FileDoc.fromUri(fileUri, false), filter) - fun getLibArchiveFilesName( + fun getArchiveFilesName( fileDoc: FileDoc, filter: ((String) -> Boolean)? = null ): List { @@ -126,31 +91,6 @@ object ArchiveUtils { } - fun getArchiveFilesName( - fileDoc: FileDoc, - filter: ((String) -> Boolean)? = null - ): List { - val name = fileDoc.name - checkAchieve(name) - return fileDoc.openInputStream().getOrThrow().use { - when { - name.endsWith(".rar", ignoreCase = true) -> { - RarUtils.getFilesName(it, filter) - } - - name.endsWith(".zip", ignoreCase = true) -> { - ZipUtils.getFilesName(it, filter) - } - - name.endsWith(".7z", ignoreCase = true) -> { - SevenZipUtils.getFilesName(it, filter) - } - - else -> emptyList() - } - } - } - fun isArchive(name: String): Boolean { return archiveFileRegex.matches(name) } diff --git a/app/src/main/java/io/legado/app/utils/compress/LibArchiveUtils.kt b/app/src/main/java/io/legado/app/utils/compress/LibArchiveUtils.kt index 1f06961dd..6f7bd9c90 100644 --- a/app/src/main/java/io/legado/app/utils/compress/LibArchiveUtils.kt +++ b/app/src/main/java/io/legado/app/utils/compress/LibArchiveUtils.kt @@ -13,6 +13,7 @@ import me.zhanghai.android.libarchive.ArchiveException import java.io.File import java.io.FileDescriptor import java.io.IOException +import java.io.InputStream import java.io.InterruptedIOException import java.nio.ByteBuffer import java.nio.channels.SeekableByteChannel @@ -23,52 +24,52 @@ import java.nio.charset.StandardCharsets object LibArchiveUtils { -// @Throws(ArchiveException::class) -// fun openArchive( -// inputStream: InputStream, -// ): Long { -// val archive: Long = Archive.readNew() -// var successful = false -// try { -// Archive.setCharset(archive, StandardCharsets.UTF_8.name().toByteArray()) -// Archive.readSupportFilterAll(archive) -// Archive.readSupportFormatAll(archive) -// Archive.readSetCallbackData(archive, null) -// val buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE) -// Archive.readSetReadCallback(archive) { _, _ -> -// buffer.clear() -// val bytesRead = try { -// inputStream.read(buffer.array()) -// } catch (e: IOException) { -// throw ArchiveException(Archive.ERRNO_FATAL, "InputStream.read", e) -// } -// if (bytesRead != -1) { -// buffer.limit(bytesRead) -// buffer -// } else { -// null -// } -// } -// Archive.readSetSkipCallback(archive) { _, _, request -> -// try { -// inputStream.skip(request) -// } catch (e: IOException) { -// throw ArchiveException(Archive.ERRNO_FATAL, "InputStream.skip", e) -// } -// } -// Archive.readOpen1(archive) -// successful = true -// return archive -// -// -// } finally { -// if (!successful) { -// Archive.free(archive) -// } -// } -// -// -// } + @Throws(ArchiveException::class) + fun openArchive( + inputStream: InputStream, + ): Long { + val archive: Long = Archive.readNew() + var successful = false + try { + Archive.setCharset(archive, StandardCharsets.UTF_8.name().toByteArray()) + Archive.readSupportFilterAll(archive) + Archive.readSupportFormatAll(archive) + Archive.readSetCallbackData(archive, null) + val buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE) + Archive.readSetReadCallback(archive) { _, _ -> + buffer.clear() + val bytesRead = try { + inputStream.read(buffer.array()) + } catch (e: IOException) { + throw ArchiveException(Archive.ERRNO_FATAL, "InputStream.read", e) + } + if (bytesRead != -1) { + buffer.limit(bytesRead) + buffer + } else { + null + } + } + Archive.readSetSkipCallback(archive) { _, _, request -> + try { + inputStream.skip(request) + } catch (e: IOException) { + throw ArchiveException(Archive.ERRNO_FATAL, "InputStream.skip", e) + } + } + Archive.readOpen1(archive) + successful = true + return archive + + + } finally { + if (!successful) { + Archive.free(archive) + } + } + + + } @Throws(ArchiveException::class) @@ -208,7 +209,9 @@ object LibArchiveUtils { } - + /** + * 解压文件 + */ @Throws(NullPointerException::class, SecurityException::class) fun unArchive( pfd: ParcelFileDescriptor, @@ -218,14 +221,16 @@ object LibArchiveUtils { return unArchive(openArchive(pfd), destDir, filter) } - + /** + * 解压 + */ @Throws(NullPointerException::class, SecurityException::class) private fun unArchive( archive: Long, destDir: File?, filter: ((String) -> Boolean)? = null ): List { - destDir ?: throw NullPointerException("解决路径不能为空") + destDir ?: throw NullPointerException("解压路径不能为空") val files = arrayListOf() @@ -275,13 +280,8 @@ object LibArchiveUtils { Archive.free(archive) } - - - - return files - } fun getFilesName(pfd: ParcelFileDescriptor, filter: ((String) -> Boolean)?): List { diff --git a/app/src/main/res/xml/pref_config_other.xml b/app/src/main/res/xml/pref_config_other.xml index 31de420b9..06299637b 100644 --- a/app/src/main/res/xml/pref_config_other.xml +++ b/app/src/main/res/xml/pref_config_other.xml @@ -98,12 +98,6 @@ android:summary="@string/pref_cronet_summary" android:title="Cronet" /> - -