实现文件类书源压缩文件解压
This commit is contained in:
@@ -24,6 +24,8 @@ object AppPattern {
|
||||
|
||||
//本地书籍支持类型
|
||||
val bookFileRegex = Regex(".*\\.(txt|epub|umd|pdf)", RegexOption.IGNORE_CASE)
|
||||
//压缩文件支持类型
|
||||
val archiveFileRegex = Regex(".*\\.(zip|rar|7z)", RegexOption.IGNORE_CASE)
|
||||
|
||||
/**
|
||||
* 所有标点
|
||||
|
||||
@@ -507,6 +507,13 @@ class BookInfoActivity :
|
||||
viewModel.importOrDownloadWebFile<Book>(webFile) {
|
||||
onClick?.invoke(it)
|
||||
}
|
||||
} else if (webFile.isSupportDecompress) {
|
||||
/* 解压筛选后再选择导入项 */
|
||||
viewModel.importOrDownloadWebFile<Uri>(webFile) { uri ->
|
||||
viewModel.deCompress(uri) {
|
||||
showDecompressFileImportAlert(it)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
alert(
|
||||
title = getString(R.string.draw),
|
||||
@@ -514,8 +521,8 @@ class BookInfoActivity :
|
||||
) {
|
||||
neutralButton(R.string.open_fun) {
|
||||
/* download only */
|
||||
viewModel.importOrDownloadWebFile<Uri>(webFile) { uri ->
|
||||
openFileUri(uri, "*/*")
|
||||
viewModel.importOrDownloadWebFile<Uri>(webFile) {
|
||||
openFileUri(it, "*/*")
|
||||
}
|
||||
}
|
||||
noButton()
|
||||
@@ -524,6 +531,22 @@ class BookInfoActivity :
|
||||
}
|
||||
}
|
||||
|
||||
private fun showDecompressFileImportAlert(
|
||||
fileDocs: List<FileDoc>
|
||||
) {
|
||||
if (fileDocs.isEmpty()) {
|
||||
toastOnUi(R.string.unsupport_archivefile_entry)
|
||||
return
|
||||
}
|
||||
val selectorNames = fileDocs.map { it.name }
|
||||
selector(
|
||||
R.string.import_select_book,
|
||||
selectorNames
|
||||
) { _, _, index ->
|
||||
viewModel.importBook(fileDocs[index])
|
||||
}
|
||||
}
|
||||
|
||||
private fun readBook(book: Book) {
|
||||
if (!viewModel.inBookshelf) {
|
||||
viewModel.saveBook(book) {
|
||||
|
||||
@@ -25,9 +25,7 @@ import io.legado.app.model.BookCover
|
||||
import io.legado.app.model.ReadBook
|
||||
import io.legado.app.model.localBook.LocalBook
|
||||
import io.legado.app.model.webBook.WebBook
|
||||
import io.legado.app.utils.isContentScheme
|
||||
import io.legado.app.utils.postEvent
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import io.legado.app.utils.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
|
||||
@@ -242,7 +240,8 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
book.downloadUrls!!.map {
|
||||
val mFileName = "${fileName}.${LocalBook.parseFileSuffix(it)}"
|
||||
val isSupportedFile = AppPattern.bookFileRegex.matches(mFileName)
|
||||
WebFile(it, mFileName, isSupportedFile)
|
||||
val isSupportDecompress = AppPattern.archiveFileRegex.matches(mFileName)
|
||||
WebFile(it, mFileName, isSupportedFile, isSupportDecompress)
|
||||
}
|
||||
}.onError {
|
||||
context.toastOnUi("LoadWebFileError\n${it.localizedMessage}")
|
||||
@@ -251,6 +250,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
/* 导入或者下载在线文件 */
|
||||
fun <T> importOrDownloadWebFile(webFile: WebFile, success: ((T) -> Unit)?) {
|
||||
bookSource ?: return
|
||||
execute {
|
||||
@@ -272,6 +272,22 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun deCompress(archiveFileUri: Uri, onSuccess: (List<FileDoc>) -> Unit) {
|
||||
execute {
|
||||
ArchiveUtils.deCompress(archiveFileUri).list {
|
||||
AppPattern.bookFileRegex.matches(it.name)
|
||||
} ?: emptyList()
|
||||
}.onError {
|
||||
context.toastOnUi("DeCompress Error:\n${it.localizedMessage}")
|
||||
}.onSuccess {
|
||||
onSuccess.invoke(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun importBook(fileDoc: FileDoc) {
|
||||
LocalBook.importFile(fileDoc.uri).let { changeToLocalBook(it) }
|
||||
}
|
||||
|
||||
fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) {
|
||||
changeSourceCoroutine?.cancel()
|
||||
changeSourceCoroutine = execute {
|
||||
@@ -398,7 +414,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
data class WebFile(
|
||||
val url: String,
|
||||
val name: String,
|
||||
val isSupported: Boolean
|
||||
// txt epub umd pdf等文件
|
||||
val isSupported: Boolean,
|
||||
// 压缩包形式的txt epub umd pdf文件
|
||||
val isSupportDecompress: Boolean
|
||||
) {
|
||||
override fun toString(): String {
|
||||
return name
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package io.legado.app.utils
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import java.io.File
|
||||
import java.util.regex.Pattern
|
||||
|
||||
import splitties.init.appCtx
|
||||
|
||||
/* 自动判断压缩文件后缀 然后再调用具体的实现 */
|
||||
object ArchiveUtils {
|
||||
|
||||
// 临时目录 下次启动自动删除
|
||||
val TEMP_PATH: String by lazy {
|
||||
appCtx.externalCache.getFile("ArchiveTemp").createFolderReplace().absolutePath
|
||||
}
|
||||
|
||||
val ZIP_REGEX = Regex(".*\\.zip", RegexOption.IGNORE_CASE)
|
||||
val RAR_REGEX = Regex(".*\\.rar", RegexOption.IGNORE_CASE)
|
||||
val SERVEZ_REGEX = Regex(".*\\.7z", RegexOption.IGNORE_CASE)
|
||||
|
||||
fun deCompress(
|
||||
archiveUri: Uri,
|
||||
path: String? = TEMP_PATH
|
||||
): FileDoc {
|
||||
return deCompress(FileDoc.fromUri(archiveUri, false), path)
|
||||
}
|
||||
|
||||
fun deCompress(
|
||||
archivePath: String,
|
||||
path: String? = TEMP_PATH
|
||||
): FileDoc {
|
||||
return deCompress(Uri.parse(archivePath)), false), path)
|
||||
}
|
||||
|
||||
fun deCompress(
|
||||
archiveFile: File,
|
||||
path: String? = TEMP_PATH
|
||||
): FileDoc {
|
||||
return deCompress(FileDoc.fromFile(archiveFile), path)
|
||||
}
|
||||
|
||||
fun deCompress(
|
||||
archiveDoc: DocumentFile,
|
||||
path: String? = TEMP_PATH
|
||||
): FileDoc {
|
||||
return deCompress(FileDoc.fromDocumentFile(archiveDoc), path)
|
||||
}
|
||||
|
||||
fun deCompress(
|
||||
archiveFileDoc: FileDoc,
|
||||
path: String? = TEMP_PATH
|
||||
): FileDoc {
|
||||
if (archiveFileDoc.isDir) throw IllegalArgumentException("Unexpected Folder input")
|
||||
val name = archiveFileDoc.name
|
||||
archiveFileDoc.uri.inputStream(appCtx).getOrThrow().use {
|
||||
when {
|
||||
ZIP_REGEX.matches(name) -> ZipUtils.unZipToPath(it, path)
|
||||
RAR_REGEX.matches(name) -> RarUtils.unRarToPath(it, path)
|
||||
SERVEZ_REGEX.matches(name) -> SevenZipUtils.un7zToPath(it, path)
|
||||
else -> throw IllegalArgumentException("Unexpected archive format")
|
||||
}
|
||||
}
|
||||
return getCacheFolderFileDoc(name, path)
|
||||
}
|
||||
|
||||
private fun getCacheFolderFileDoc(
|
||||
archiveName: String,
|
||||
workPath: String
|
||||
): FileDoc {
|
||||
return FileDoc.fromUri(Uri.parse(workPath), true)
|
||||
.createFolderIfNotExist(MD5Utils.md5Encode16(archiveName))
|
||||
}
|
||||
}
|
||||
@@ -1080,4 +1080,5 @@
|
||||
<string name="keep_group">保留分组</string>
|
||||
<string name="server_config">服务器配置</string>
|
||||
<string name="sure_upload">Remote webDav url exists, Continue?</string>
|
||||
<string name="unsupport_archivefile_entry">Cannot find supported files in archive</string>
|
||||
</resources>
|
||||
|
||||
@@ -1083,4 +1083,5 @@
|
||||
<string name="keep_group">保留分组</string>
|
||||
<string name="server_config">服务器配置</string>
|
||||
<string name="sure_upload">Remote webDav url exists, Continue?</string>
|
||||
<string name="unsupport_archivefile_entry">Cannot find supported files in archive</string>
|
||||
</resources>
|
||||
|
||||
@@ -1083,4 +1083,5 @@
|
||||
<string name="keep_group">保留分组</string>
|
||||
<string name="server_config">服务器配置</string>
|
||||
<string name="sure_upload">Remote webDav url exists, Continue?</string>
|
||||
<string name="unsupport_archivefile_entry">Cannot find supported files in archive</string>
|
||||
</resources>
|
||||
|
||||
@@ -1080,4 +1080,5 @@
|
||||
<string name="keep_group">保留分组</string>
|
||||
<string name="server_config">服务器配置</string>
|
||||
<string name="sure_upload">远程webDav链接已存在,是否继续</string>
|
||||
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
|
||||
</resources>
|
||||
|
||||
@@ -1082,4 +1082,5 @@
|
||||
<string name="keep_group">保留分组</string>
|
||||
<string name="server_config">服务器配置</string>
|
||||
<string name="sure_upload">远程webDav链接已存在,是否继续</string>
|
||||
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
|
||||
</resources>
|
||||
|
||||
@@ -1082,4 +1082,5 @@
|
||||
<string name="keep_group">保留分组</string>
|
||||
<string name="server_config">服务器配置</string>
|
||||
<string name="sure_upload">远程webDav链接已存在,是否继续</string>
|
||||
<string name="unsupport_archivefile_entry">压缩文件内没有支持的文件</string>
|
||||
</resources>
|
||||
|
||||
@@ -1083,4 +1083,5 @@
|
||||
<string name="keep_group">Keep group</string>
|
||||
<string name="server_config">服务器配置</string>
|
||||
<string name="sure_upload">Remote webDav url exists, Continue?</string>
|
||||
<string name="unsupport_archivefile_entry">Cannot find supported files in archive</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user