From 1cb017c70004d72486228c458a2f9df7e299ace1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 13 Mar 2023 17:07:59 +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 --- .../main/java/io/legado/app/help/AppWebDav.kt | 3 +- .../legado/app/help/config/ReadBookConfig.kt | 2 +- .../io/legado/app/help/storage/Restore.kt | 2 +- .../io/legado/app/utils/compress/ZipUtils.kt | 154 +++--------------- 4 files changed, 30 insertions(+), 131 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/AppWebDav.kt b/app/src/main/java/io/legado/app/help/AppWebDav.kt index a5878dfc3..6f8ff916e 100644 --- a/app/src/main/java/io/legado/app/help/AppWebDav.kt +++ b/app/src/main/java/io/legado/app/help/AppWebDav.kt @@ -28,6 +28,7 @@ import kotlinx.coroutines.ensureActive import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import splitties.init.appCtx +import java.io.File import java.util.* import kotlin.coroutines.coroutineContext @@ -141,7 +142,7 @@ object AppWebDav { val webDav = WebDav(rootWebDavUrl + name, it) webDav.downloadTo(Backup.zipFilePath, true) FileUtils.delete(Backup.backupPath) - ZipUtils.unzipFile(Backup.zipFilePath, Backup.backupPath) + ZipUtils.unZipToPath(File(Backup.zipFilePath), Backup.backupPath) Restore.restoreDatabase() Restore.restoreConfig() } diff --git a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt index 076ef047f..4f25352a8 100644 --- a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt @@ -389,7 +389,7 @@ object ReadBookConfig { val configDir = appCtx.externalCache.getFile("readConfig") configDir.createFolderReplace() @Suppress("BlockingMethodInNonBlockingContext") - ZipUtils.unzipFile(zipFile, configDir) + ZipUtils.unZipToPath(zipFile, configDir) val configFile = configDir.getFile(configFileName) val config: Config = GSON.fromJsonObject(configFile.readText()).getOrThrow() if (config.textFont.isNotEmpty()) { diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 4690e77df..2ac1c049b 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -42,7 +42,7 @@ object Restore { ZipUtils.unZipToPath(it, Backup.backupPath) } } else { - ZipUtils.unzipFile(uri.path!!, Backup.backupPath) + ZipUtils.unZipToPath(File(uri.path!!), Backup.backupPath) } }.onFailure { AppLog.put("恢复复制文件出错\n${it.localizedMessage}", it) diff --git a/app/src/main/java/io/legado/app/utils/compress/ZipUtils.kt b/app/src/main/java/io/legado/app/utils/compress/ZipUtils.kt index f5278bf25..2c64d82d5 100644 --- a/app/src/main/java/io/legado/app/utils/compress/ZipUtils.kt +++ b/app/src/main/java/io/legado/app/utils/compress/ZipUtils.kt @@ -177,18 +177,39 @@ object ZipUtils { } @Throws(SecurityException::class) - fun unZipToPath(inputStream: InputStream, path: String) { - ZipInputStream(inputStream).use { + fun unZipToPath(file: File, path: String) { + FileInputStream(file).use { unZipToPath(it, path) } } @Throws(SecurityException::class) - fun unZipToPath(zipInputStream: ZipInputStream, path: String) { + fun unZipToPath(file: File, dir: File) { + FileInputStream(file).use { + unZipToPath(it, dir) + } + } + + @Throws(SecurityException::class) + fun unZipToPath(inputStream: InputStream, path: String) { + ZipInputStream(inputStream).use { + unZipToPath(it, File(path)) + } + } + + @Throws(SecurityException::class) + fun unZipToPath(inputStream: InputStream, dir: File) { + ZipInputStream(inputStream).use { + unZipToPath(it, dir) + } + } + + @Throws(SecurityException::class) + fun unZipToPath(zipInputStream: ZipInputStream, dir: File) { var entry: ZipEntry? while (zipInputStream.nextEntry.also { entry = it } != null) { - val entryFile = File(path, entry!!.name) - if (!entryFile.canonicalPath.startsWith(path)) { + val entryFile = File(dir, entry!!.name) + if (!entryFile.canonicalPath.startsWith(dir.canonicalPath)) { throw SecurityException("压缩文件只能解压到指定路径") } if (entry!!.isDirectory) { @@ -211,129 +232,6 @@ object ZipUtils { } } - /** - * Unzip the file. - * - * @param zipFilePath The path of ZIP file. - * @param destDirPath The path of destination directory. - * @return the unzipped files - * @throws IOException if unzip unsuccessfully - */ - @Throws(IOException::class) - fun unzipFile(zipFilePath: String, destDirPath: String): List? { - return unzipFileByKeyword(zipFilePath, destDirPath, null) - } - - /** - * Unzip the file. - * - * @param zipFile The ZIP file. - * @param destDir The destination directory. - * @return the unzipped files - * @throws IOException if unzip unsuccessfully - */ - @Throws(IOException::class) - fun unzipFile( - zipFile: File, - destDir: File - ): List? { - return unzipFileByKeyword(zipFile, destDir, null) - } - - /** - * Unzip the file by keyword. - * - * @param zipFilePath The path of ZIP file. - * @param destDirPath The path of destination directory. - * @param keyword The keyboard. - * @return the unzipped files - * @throws IOException if unzip unsuccessfully - */ - @Throws(IOException::class) - fun unzipFileByKeyword( - zipFilePath: String, - destDirPath: String, - keyword: String? - ): List? { - return unzipFileByKeyword( - getFileByPath(zipFilePath), - getFileByPath(destDirPath), - keyword - ) - } - - /** - * Unzip the file by keyword. - * - * @param zipFile The ZIP file. - * @param destDir The destination directory. - * @param keyword The keyboard. - * @return the unzipped files - * @throws IOException if unzip unsuccessfully - */ - @Throws(IOException::class) - fun unzipFileByKeyword( - zipFile: File?, - destDir: File?, - keyword: String? - ): List? { - if (zipFile == null || destDir == null) return null - val files = ArrayList() - val zip = ZipFile(zipFile) - val entries = zip.entries() - zip.use { - if (isSpace(keyword)) { - while (entries.hasMoreElements()) { - val entry = entries.nextElement() as ZipEntry - val entryName = entry.name - if (entryName.contains("../")) { - DebugLog.e(javaClass.name, "entryName: $entryName is dangerous!") - continue - } - if (!unzipChildFile(destDir, files, zip, entry, entryName)) return files - } - } else { - while (entries.hasMoreElements()) { - val entry = entries.nextElement() as ZipEntry - val entryName = entry.name - if (entryName.contains("../")) { - DebugLog.e(javaClass.name, "entryName: $entryName is dangerous!") - continue - } - if (entryName.contains(keyword!!)) { - if (!unzipChildFile(destDir, files, zip, entry, entryName)) return files - } - } - } - } - return files - } - - @Throws(IOException::class, SecurityException::class) - private fun unzipChildFile( - destDir: File, - files: MutableList, - zip: ZipFile, - entry: ZipEntry, - name: String - ): Boolean { - val file = File(destDir, name) - if (!file.canonicalPath.startsWith(destDir.canonicalPath)) { - throw SecurityException("压缩文件只能解压到指定路径") - } - files.add(file) - if (entry.isDirectory) { - return createOrExistsDir(file) - } else { - if (!createOrExistsFile(file)) return false - zip.getInputStream(entry).use { input -> - FileOutputStream(file).use { out -> - input.copyTo(out) - } - } - } - return true - } /** * Return the files' path in ZIP file.