修复恢复备份用的zip库不支持5.0,6.0的问题
This commit is contained in:
@@ -44,12 +44,13 @@ object Restore {
|
|||||||
} else {
|
} else {
|
||||||
ZipUtils.unZipToPath(File(uri.path!!), Backup.backupPath)
|
ZipUtils.unZipToPath(File(uri.path!!), Backup.backupPath)
|
||||||
}
|
}
|
||||||
|
}.onSuccess {
|
||||||
|
restoreDatabase()
|
||||||
|
restoreConfig()
|
||||||
|
LocalConfig.lastBackup = System.currentTimeMillis()
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
AppLog.put("恢复复制文件出错\n${it.localizedMessage}", it)
|
AppLog.put("复制解压文件出错\n${it.localizedMessage}", it)
|
||||||
}
|
}
|
||||||
restoreDatabase()
|
|
||||||
restoreConfig()
|
|
||||||
LocalConfig.lastBackup = System.currentTimeMillis()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun restoreDatabase(path: String = Backup.backupPath) {
|
suspend fun restoreDatabase(path: String = Backup.backupPath) {
|
||||||
|
|||||||
@@ -78,17 +78,18 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
|
|||||||
execute {
|
execute {
|
||||||
importSource(text)
|
importSource(text)
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
it?.let(finally) ?: context.toastOnUi("格式不对")
|
finally.invoke(it)
|
||||||
}.onError {
|
}.onError {
|
||||||
context.toastOnUi(it.localizedMessage ?: "Error")
|
context.toastOnUi(it.localizedMessage ?: "Error")
|
||||||
|
it.printOnDebug()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun importSource(text: String): BookSource? {
|
suspend fun importSource(text: String): BookSource {
|
||||||
return when {
|
return when {
|
||||||
text.isAbsUrl() -> {
|
text.isAbsUrl() -> {
|
||||||
val text1 = okHttpClient.newCallStrResponse { url(text) }.body
|
val text1 = okHttpClient.newCallStrResponse { url(text) }.body
|
||||||
text1?.let { importSource(text1) }
|
importSource(text1!!)
|
||||||
}
|
}
|
||||||
text.isJsonArray() -> {
|
text.isJsonArray() -> {
|
||||||
if (text.contains("ruleSearchUrl") || text.contains("ruleFindUrl")) {
|
if (text.contains("ruleSearchUrl") || text.contains("ruleFindUrl")) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package io.legado.app.utils.compress
|
package io.legado.app.utils.compress
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
import io.legado.app.utils.DebugLog
|
import io.legado.app.utils.DebugLog
|
||||||
import io.legado.app.utils.printOnDebug
|
import io.legado.app.utils.printOnDebug
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
@@ -193,16 +194,36 @@ object ZipUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Throws(SecurityException::class)
|
@Throws(SecurityException::class)
|
||||||
fun unZipToPath(inputStream: InputStream, path: String, filter: ((String) -> Boolean)? = null): List<File> {
|
fun unZipToPath(
|
||||||
return ZipArchiveInputStream(inputStream).use {
|
inputStream: InputStream,
|
||||||
unZipToPath(it, File(path), filter)
|
path: String,
|
||||||
|
filter: ((String) -> Boolean)? = null
|
||||||
|
): List<File> {
|
||||||
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
ZipArchiveInputStream(inputStream).use {
|
||||||
|
unZipToPath(it, File(path), filter)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ZipInputStream(inputStream).use {
|
||||||
|
unZipToPath(it, File(path), filter)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(SecurityException::class)
|
@Throws(SecurityException::class)
|
||||||
fun unZipToPath(inputStream: InputStream, dir: File, filter: ((String) -> Boolean)? = null): List<File> {
|
fun unZipToPath(
|
||||||
return ZipArchiveInputStream(inputStream).use {
|
inputStream: InputStream,
|
||||||
unZipToPath(it, dir, filter)
|
dir: File,
|
||||||
|
filter: ((String) -> Boolean)? = null
|
||||||
|
): List<File> {
|
||||||
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
ZipArchiveInputStream(inputStream).use {
|
||||||
|
unZipToPath(it, dir, filter)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ZipInputStream(inputStream).use {
|
||||||
|
unZipToPath(it, dir, filter)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,6 +264,44 @@ object ZipUtils {
|
|||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Throws(SecurityException::class)
|
||||||
|
private fun unZipToPath(
|
||||||
|
zipInputStream: ZipInputStream,
|
||||||
|
dir: File,
|
||||||
|
filter: ((String) -> Boolean)? = null
|
||||||
|
): List<File> {
|
||||||
|
val files = arrayListOf<File>()
|
||||||
|
var entry: ZipEntry?
|
||||||
|
while (zipInputStream.nextEntry.also { entry = it } != null) {
|
||||||
|
val entryName = entry!!.name
|
||||||
|
val entryFile = File(dir, entryName)
|
||||||
|
if (!entryFile.canonicalPath.startsWith(dir.canonicalPath)) {
|
||||||
|
throw SecurityException("压缩文件只能解压到指定路径")
|
||||||
|
}
|
||||||
|
if (entry!!.isDirectory) {
|
||||||
|
if (!entryFile.exists()) {
|
||||||
|
entryFile.mkdirs()
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (entryFile.parentFile?.exists() != true) {
|
||||||
|
entryFile.parentFile?.mkdirs()
|
||||||
|
}
|
||||||
|
if (filter != null && !filter.invoke(entryName)) continue
|
||||||
|
if (!entryFile.exists()) {
|
||||||
|
entryFile.createNewFile()
|
||||||
|
entryFile.setReadable(true)
|
||||||
|
entryFile.setExecutable(true)
|
||||||
|
}
|
||||||
|
FileOutputStream(entryFile).use {
|
||||||
|
zipInputStream.copyTo(it)
|
||||||
|
files.add(entryFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
/* 遍历目录获取所有文件名 */
|
/* 遍历目录获取所有文件名 */
|
||||||
@Throws(SecurityException::class)
|
@Throws(SecurityException::class)
|
||||||
fun getFilesName(
|
fun getFilesName(
|
||||||
@@ -267,7 +326,7 @@ object ZipUtils {
|
|||||||
}
|
}
|
||||||
val fileName = entry!!.name
|
val fileName = entry!!.name
|
||||||
if (filter != null && filter.invoke(fileName))
|
if (filter != null && filter.invoke(fileName))
|
||||||
fileNames.add(fileName)
|
fileNames.add(fileName)
|
||||||
}
|
}
|
||||||
return fileNames
|
return fileNames
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user