diff --git a/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveEntry.kt b/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveEntry.kt new file mode 100644 index 0000000000..6dd6f1386d --- /dev/null +++ b/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveEntry.kt @@ -0,0 +1,6 @@ +package tachiyomi.core.common.archive + +class ArchiveEntry( + val name: String, + val isFile: Boolean, +) diff --git a/core/common/src/main/kotlin/tachiyomi/core/common/archive/Archive.kt b/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveInputStream.kt similarity index 80% rename from core/common/src/main/kotlin/tachiyomi/core/common/archive/Archive.kt rename to core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveInputStream.kt index 623cff0569..d34ab54809 100644 --- a/core/common/src/main/kotlin/tachiyomi/core/common/archive/Archive.kt +++ b/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveInputStream.kt @@ -1,11 +1,11 @@ -package tachiyomi.core.archive +package tachiyomi.core.common.archive import me.zhanghai.android.libarchive.Archive import me.zhanghai.android.libarchive.ArchiveEntry import java.io.InputStream import java.nio.ByteBuffer -class Archive(buffer: Long, size: Long) : InputStream() { +class ArchiveInputStream(buffer: Long, size: Long) : InputStream() { private val archive = Archive.readNew() init { @@ -14,7 +14,7 @@ class Archive(buffer: Long, size: Long) : InputStream() { Archive.readSupportFilterAll(archive) Archive.readSupportFormatAll(archive) Archive.readOpenMemoryUnsafe(archive, buffer, size) - } catch (e: Throwable) { + } catch (e: Exception) { close() throw e } @@ -43,13 +43,9 @@ class Archive(buffer: Long, size: Long) : InputStream() { Archive.readFree(archive) } - fun readEntry(): Entry? { - val entry = Archive.readNextHeader(archive) - if (entry == 0L) return null + fun getNextEntry() = Archive.readNextHeader(archive).takeUnless { it == 0L }?.let { entry -> val name = ArchiveEntry.pathnameUtf8(entry) ?: ArchiveEntry.pathname(entry)?.decodeToString() ?: return null val isFile = ArchiveEntry.filetype(entry) == ArchiveEntry.AE_IFREG - return Entry(name, isFile) + ArchiveEntry(name, isFile) } - - class Entry(val name: String, val isFile: Boolean) } diff --git a/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveReader.kt b/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveReader.kt index f1caad4dd6..29c2d8c855 100644 --- a/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveReader.kt +++ b/core/common/src/main/kotlin/tachiyomi/core/common/archive/ArchiveReader.kt @@ -1,4 +1,4 @@ -package tachiyomi.core.archive +package tachiyomi.core.common.archive import android.content.Context import android.os.ParcelFileDescriptor @@ -13,19 +13,19 @@ class ArchiveReader(pfd: ParcelFileDescriptor) : Closeable { val size = pfd.statSize val address = Os.mmap(0, size, OsConstants.PROT_READ, OsConstants.MAP_PRIVATE, pfd.fileDescriptor, 0) - inline fun useEntries(block: (Sequence) -> T): T = - Archive(address, size).use { block(generateSequence { it.readEntry() }) } + inline fun useEntries(block: (Sequence) -> T): T = + ArchiveInputStream(address, size).use { block(generateSequence { it.getNextEntry() }) } fun getInputStream(entryName: String): InputStream? { - val archive = Archive(address, size) + val archive = ArchiveInputStream(address, size) try { while (true) { - val entry = archive.readEntry() ?: break + val entry = archive.getNextEntry() ?: break if (entry.name == entryName) { return archive } } - } catch (e: Throwable) { + } catch (e: Exception) { archive.close() throw e } diff --git a/core/common/src/main/kotlin/tachiyomi/core/common/archive/ZipWriter.kt b/core/common/src/main/kotlin/tachiyomi/core/common/archive/ZipWriter.kt index 90745b5d00..5e276a275f 100644 --- a/core/common/src/main/kotlin/tachiyomi/core/common/archive/ZipWriter.kt +++ b/core/common/src/main/kotlin/tachiyomi/core/common/archive/ZipWriter.kt @@ -1,4 +1,4 @@ -package tachiyomi.core.archive +package tachiyomi.core.common.archive import android.content.Context import android.system.Os