mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -49,6 +49,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
|
||||
class BookInfoActivity :
|
||||
VMBaseActivity<ActivityBookInfoBinding, BookInfoViewModel>(toolBarTheme = Theme.Dark),
|
||||
@@ -559,18 +560,18 @@ class BookInfoActivity :
|
||||
}
|
||||
|
||||
private fun showDecompressFileImportAlert(
|
||||
fileDocs: List<FileDoc>
|
||||
files: List<File>
|
||||
) {
|
||||
if (fileDocs.isEmpty()) {
|
||||
if (files.isEmpty()) {
|
||||
toastOnUi(R.string.unsupport_archivefile_entry)
|
||||
return
|
||||
}
|
||||
val selectorNames = fileDocs.map { it.name }
|
||||
val selectorNames = files.map { it.name }
|
||||
selector(
|
||||
R.string.import_select_book,
|
||||
selectorNames
|
||||
) { _, _, index ->
|
||||
viewModel.importBook(fileDocs[index])
|
||||
viewModel.importBook(files[index])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ import io.legado.app.model.webBook.WebBook
|
||||
import io.legado.app.utils.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
|
||||
class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
val bookData = MutableLiveData<Book>()
|
||||
@@ -279,11 +281,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun deCompress(archiveFileUri: Uri, onSuccess: (List<FileDoc>) -> Unit) {
|
||||
fun deCompress(archiveFileUri: Uri, onSuccess: (List<File>) -> Unit) {
|
||||
execute {
|
||||
ArchiveUtils.deCompress(archiveFileUri).list {
|
||||
ArchiveUtils.deCompress(archiveFileUri).filter {
|
||||
AppPattern.bookFileRegex.matches(it.name)
|
||||
} ?: emptyList()
|
||||
}
|
||||
}.onError {
|
||||
context.toastOnUi("DeCompress Error:\n${it.localizedMessage}")
|
||||
}.onSuccess {
|
||||
@@ -291,10 +293,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun importBook(fileDoc: FileDoc) {
|
||||
fun importBook(file: File) {
|
||||
val uri = LocalBook.saveBookFile(
|
||||
fileDoc.uri.inputStream(context).getOrThrow(),
|
||||
fileDoc.name
|
||||
FileInputStream(file),
|
||||
file.name
|
||||
)
|
||||
changeToLocalBook(LocalBook.importFile(uri))
|
||||
}
|
||||
|
||||
@@ -20,40 +20,40 @@ object ArchiveUtils {
|
||||
fun deCompress(
|
||||
archiveUri: Uri,
|
||||
path: String = TEMP_PATH
|
||||
): FileDoc {
|
||||
): List<File> {
|
||||
return deCompress(FileDoc.fromUri(archiveUri, false), path)
|
||||
}
|
||||
|
||||
fun deCompress(
|
||||
archivePath: String,
|
||||
path: String = TEMP_PATH
|
||||
): FileDoc {
|
||||
): List<File> {
|
||||
return deCompress(Uri.parse(archivePath), path)
|
||||
}
|
||||
|
||||
fun deCompress(
|
||||
archiveFile: File,
|
||||
path: String = TEMP_PATH
|
||||
): FileDoc {
|
||||
): List<File> {
|
||||
return deCompress(FileDoc.fromFile(archiveFile), path)
|
||||
}
|
||||
|
||||
fun deCompress(
|
||||
archiveDoc: DocumentFile,
|
||||
path: String = TEMP_PATH
|
||||
): FileDoc {
|
||||
): List<File> {
|
||||
return deCompress(FileDoc.fromDocumentFile(archiveDoc), path)
|
||||
}
|
||||
|
||||
fun deCompress(
|
||||
archiveFileDoc: FileDoc,
|
||||
path: String = TEMP_PATH
|
||||
): FileDoc {
|
||||
): List<File> {
|
||||
if (archiveFileDoc.isDir) throw IllegalArgumentException("Unexpected Folder input")
|
||||
val name = archiveFileDoc.name
|
||||
val workPathFileDoc = getCacheFolderFileDoc(name, path)
|
||||
val workPath = workPathFileDoc.toString()
|
||||
archiveFileDoc.uri.inputStream(appCtx).getOrThrow().use {
|
||||
return archiveFileDoc.uri.inputStream(appCtx).getOrThrow().use {
|
||||
when {
|
||||
name.endsWith(".zip", ignoreCase = true) -> ZipUtils.unZipToPath(it, workPath)
|
||||
name.endsWith(".rar", ignoreCase = true) -> RarUtils.unRarToPath(it, workPath)
|
||||
@@ -61,7 +61,6 @@ object ArchiveUtils {
|
||||
else -> throw IllegalArgumentException("Unexpected archive format")
|
||||
}
|
||||
}
|
||||
return workPathFileDoc
|
||||
}
|
||||
|
||||
private fun getCacheFolderFileDoc(
|
||||
|
||||
@@ -13,57 +13,57 @@ import java.io.InputStream
|
||||
object RarUtils {
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(inputStream: InputStream, path: String) {
|
||||
unRarToPath(inputStream, File(path))
|
||||
fun unRarToPath(inputStream: InputStream, path: String): List<File> {
|
||||
return unRarToPath(inputStream, File(path))
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(byteArray: ByteArray, path: String) {
|
||||
unRarToPath(byteArray, File(path))
|
||||
fun unRarToPath(byteArray: ByteArray, path: String): List<File> {
|
||||
return unRarToPath(byteArray, File(path))
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(zipPath: String, path: String) {
|
||||
unRarToPath(zipPath, File(path))
|
||||
fun unRarToPath(zipPath: String, path: String): List<File> {
|
||||
return unRarToPath(zipPath, File(path))
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(file: File, path: String) {
|
||||
unRarToPath(file, File(path))
|
||||
fun unRarToPath(file: File, path: String): List<File> {
|
||||
return unRarToPath(file, File(path))
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(inputStream: InputStream, destDir: File?) {
|
||||
Archive(inputStream).use {
|
||||
unRarToPath(it, destDir)
|
||||
}
|
||||
inputStream.close()
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(byteArray: ByteArray, destDir: File?) {
|
||||
Archive(ByteArrayInputStream(byteArray)).use {
|
||||
fun unRarToPath(inputStream: InputStream, destDir: File?): List<File> {
|
||||
return Archive(inputStream).use {
|
||||
unRarToPath(it, destDir)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(filePath: String, destDir: File?) {
|
||||
Archive(File(filePath)).use {
|
||||
fun unRarToPath(byteArray: ByteArray, destDir: File?): List<File> {
|
||||
return Archive(ByteArrayInputStream(byteArray)).use {
|
||||
unRarToPath(it, destDir)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(file: File, destDir: File?) {
|
||||
Archive(file).use {
|
||||
fun unRarToPath(filePath: String, destDir: File?): List<File> {
|
||||
return Archive(File(filePath)).use {
|
||||
unRarToPath(it, destDir)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(archive: Archive, destDir: File?) {
|
||||
fun unRarToPath(file: File, destDir: File?): List<File> {
|
||||
return Archive(file).use {
|
||||
unRarToPath(it, destDir)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun unRarToPath(archive: Archive, destDir: File?): List<File> {
|
||||
destDir ?: throw NullPointerException("解决路径不能为空")
|
||||
val files = arrayListOf<File>()
|
||||
var entry: FileHeader?
|
||||
while (archive.nextFileHeader().also { entry = it } != null) {
|
||||
val entryFile = File(destDir, entry!!.fileName)
|
||||
@@ -86,8 +86,10 @@ object RarUtils {
|
||||
}
|
||||
FileOutputStream(entryFile).use {
|
||||
archive.getInputStream(entry).copyTo(it)
|
||||
files.add(entryFile)
|
||||
}
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,59 +17,60 @@ import java.nio.channels.FileChannel
|
||||
object SevenZipUtils {
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(inputStream: InputStream, path: String) {
|
||||
un7zToPath(inputStream, File(path))
|
||||
fun un7zToPath(inputStream: InputStream, path: String): List<File> {
|
||||
return un7zToPath(inputStream, File(path))
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(byteArray: ByteArray, path: String) {
|
||||
un7zToPath(byteArray, File(path))
|
||||
fun un7zToPath(byteArray: ByteArray, path: String): List<File> {
|
||||
return un7zToPath(byteArray, File(path))
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(pfd: ParcelFileDescriptor, path: String) {
|
||||
un7zToPath(pfd, File(path))
|
||||
fun un7zToPath(pfd: ParcelFileDescriptor, path: String): List<File> {
|
||||
return un7zToPath(pfd, File(path))
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(fileChannel: FileChannel, path: String) {
|
||||
un7zToPath(fileChannel, File(path))
|
||||
fun un7zToPath(fileChannel: FileChannel, path: String): List<File> {
|
||||
return un7zToPath(fileChannel, File(path))
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(inputStream: InputStream, destDir: File?) {
|
||||
un7zToPath(SevenZFile(SeekableInMemoryByteChannel(inputStream.readBytes())), destDir)
|
||||
fun un7zToPath(inputStream: InputStream, destDir: File?): List<File> {
|
||||
return un7zToPath(SevenZFile(SeekableInMemoryByteChannel(inputStream.readBytes())), destDir)
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(byteArray: ByteArray, destDir: File?) {
|
||||
un7zToPath(SevenZFile(SeekableInMemoryByteChannel(byteArray)), destDir)
|
||||
fun un7zToPath(byteArray: ByteArray, destDir: File?): List<File> {
|
||||
return un7zToPath(SevenZFile(SeekableInMemoryByteChannel(byteArray)), destDir)
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(pfd: ParcelFileDescriptor, destDir: File?) {
|
||||
un7zToPath(SevenZFile(ParcelFileDescriptorChannel(pfd)), destDir)
|
||||
fun un7zToPath(pfd: ParcelFileDescriptor, destDir: File?): List<File> {
|
||||
return un7zToPath(SevenZFile(ParcelFileDescriptorChannel(pfd)), destDir)
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(fileChannel: FileChannel, destDir: File?) {
|
||||
un7zToPath(SevenZFile(fileChannel), destDir)
|
||||
fun un7zToPath(fileChannel: FileChannel, destDir: File?): List<File> {
|
||||
return un7zToPath(SevenZFile(fileChannel), destDir)
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(file: File, destDir: File?) {
|
||||
un7zToPath(SevenZFile(file), destDir)
|
||||
fun un7zToPath(file: File, destDir: File?): List<File> {
|
||||
return un7zToPath(SevenZFile(file), destDir)
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(filePath: String, destDir: File?) {
|
||||
un7zToPath(SevenZFile(File(filePath)), destDir)
|
||||
fun un7zToPath(filePath: String, destDir: File?): List<File> {
|
||||
return un7zToPath(SevenZFile(File(filePath)), destDir)
|
||||
}
|
||||
|
||||
@Throws(NullPointerException::class, SecurityException::class)
|
||||
fun un7zToPath(sevenZFile: SevenZFile, destDir: File?) {
|
||||
fun un7zToPath(sevenZFile: SevenZFile, destDir: File?): List<File> {
|
||||
destDir ?: throw NullPointerException("解决路径不能为空")
|
||||
val files = arrayListOf<File>()
|
||||
var entry: SevenZArchiveEntry?
|
||||
while (sevenZFile.nextEntry.also { entry = it } != null) {
|
||||
val entryFile = File(destDir, entry!!.name)
|
||||
@@ -92,7 +93,9 @@ object SevenZipUtils {
|
||||
}
|
||||
FileOutputStream(entryFile).use {
|
||||
sevenZFile.getInputStream(entry).copyTo(it)
|
||||
files.add(entryFile)
|
||||
}
|
||||
}
|
||||
return files
|
||||
}
|
||||
}
|
||||
@@ -177,35 +177,36 @@ object ZipUtils {
|
||||
}
|
||||
|
||||
@Throws(SecurityException::class)
|
||||
fun unZipToPath(file: File, path: String) {
|
||||
FileInputStream(file).use {
|
||||
fun unZipToPath(file: File, path: String): List<File> {
|
||||
return FileInputStream(file).use {
|
||||
unZipToPath(it, path)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(SecurityException::class)
|
||||
fun unZipToPath(file: File, dir: File) {
|
||||
FileInputStream(file).use {
|
||||
fun unZipToPath(file: File, dir: File): List<File> {
|
||||
return FileInputStream(file).use {
|
||||
unZipToPath(it, dir)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(SecurityException::class)
|
||||
fun unZipToPath(inputStream: InputStream, path: String) {
|
||||
ZipInputStream(inputStream).use {
|
||||
fun unZipToPath(inputStream: InputStream, path: String): List<File> {
|
||||
return ZipInputStream(inputStream).use {
|
||||
unZipToPath(it, File(path))
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(SecurityException::class)
|
||||
fun unZipToPath(inputStream: InputStream, dir: File) {
|
||||
ZipInputStream(inputStream).use {
|
||||
fun unZipToPath(inputStream: InputStream, dir: File): List<File> {
|
||||
return ZipInputStream(inputStream).use {
|
||||
unZipToPath(it, dir)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(SecurityException::class)
|
||||
fun unZipToPath(zipInputStream: ZipInputStream, dir: File) {
|
||||
fun unZipToPath(zipInputStream: ZipInputStream, dir: File): List<File> {
|
||||
val files = arrayListOf<File>()
|
||||
var entry: ZipEntry?
|
||||
while (zipInputStream.nextEntry.also { entry = it } != null) {
|
||||
val entryFile = File(dir, entry!!.name)
|
||||
@@ -228,8 +229,10 @@ object ZipUtils {
|
||||
}
|
||||
FileOutputStream(entryFile).use {
|
||||
zipInputStream.copyTo(it)
|
||||
files.add(entryFile)
|
||||
}
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user