From bb2f195158e0c5cdf759ec305fb9bf6dca722378 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Wed, 20 Sep 2023 23:30:38 +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/utils/compress/LibArchiveUtils.kt | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) 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 3eede4120..9d070a85f 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 @@ -10,7 +10,7 @@ import io.legado.app.lib.icu4j.CharsetDetector import me.zhanghai.android.libarchive.Archive import me.zhanghai.android.libarchive.ArchiveEntry import me.zhanghai.android.libarchive.ArchiveException -import splitties.init.appCtx +import okio.Buffer import java.io.File import java.io.FileDescriptor import java.io.IOException @@ -24,8 +24,6 @@ import java.nio.charset.StandardCharsets object LibArchiveUtils { - val cachePath = File(appCtx.cacheDir, "archive") - @Throws(ArchiveException::class) fun openArchive( inputStream: InputStream, @@ -153,7 +151,7 @@ object LibArchiveUtils { val buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE) Archive.readSetReadCallback( archive - ) { _1: Long, fd: Any? -> + ) { _: Long, fd: Any? -> buffer.clear() try { Os.read(fd as FileDescriptor?, buffer) @@ -167,7 +165,7 @@ object LibArchiveUtils { } Archive.readSetSkipCallback( archive - ) { _1: Long, fd: Any?, request: Long -> + ) { _: Long, fd: Any?, request: Long -> try { Os.lseek( fd as FileDescriptor?, request, OsConstants.SEEK_CUR @@ -179,7 +177,7 @@ object LibArchiveUtils { } Archive.readSetSeekCallback( archive - ) { _1: Long, fd: Any?, offset: Long, whence: Int -> + ) { _: Long, fd: Any?, offset: Long, whence: Int -> try { return@readSetSeekCallback Os.lseek( fd as FileDescriptor?, offset, whence @@ -304,18 +302,17 @@ object LibArchiveUtils { } if (entryName == path) { - cachePath.mkdirs() - val entryFile = File(cachePath, entry.toString()) - entryFile.delete() - entryFile.createNewFile() - entryFile.setReadable(true) - entryFile.setExecutable(true) - ParcelFileDescriptor.open(entryFile, ParcelFileDescriptor.MODE_WRITE_ONLY).use { - Archive.readDataIntoFd(archive, it.fd) + val byteBuffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE) + val buffer = Buffer() + while (true) { + Archive.readData(archive, byteBuffer) + byteBuffer.flip() + if (!byteBuffer.hasRemaining()) { + return buffer.readByteArray() + } + buffer.write(byteBuffer) + byteBuffer.clear() } - val bytes = entryFile.readBytes() - entryFile.delete() - return bytes } entry = Archive.readNextHeader(archive)