Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Sep 20, 2023
1 parent 6f07594 commit bb2f195
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions app/src/main/java/io/legado/app/utils/compress/LibArchiveUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,8 +24,6 @@ import java.nio.charset.StandardCharsets

object LibArchiveUtils {

val cachePath = File(appCtx.cacheDir, "archive")

@Throws(ArchiveException::class)
fun openArchive(
inputStream: InputStream,
Expand Down Expand Up @@ -153,7 +151,7 @@ object LibArchiveUtils {
val buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE)
Archive.readSetReadCallback<Any>(
archive
) { _1: Long, fd: Any? ->
) { _: Long, fd: Any? ->
buffer.clear()
try {
Os.read(fd as FileDescriptor?, buffer)
Expand All @@ -167,7 +165,7 @@ object LibArchiveUtils {
}
Archive.readSetSkipCallback<Any>(
archive
) { _1: Long, fd: Any?, request: Long ->
) { _: Long, fd: Any?, request: Long ->
try {
Os.lseek(
fd as FileDescriptor?, request, OsConstants.SEEK_CUR
Expand All @@ -179,7 +177,7 @@ object LibArchiveUtils {
}
Archive.readSetSeekCallback<Any>(
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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bb2f195

Please sign in to comment.