Skip to content

Commit

Permalink
Refine
Browse files Browse the repository at this point in the history
  • Loading branch information
FooIbar committed Jun 25, 2024
1 parent 38bb6b3 commit 348cf3e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tachiyomi.core.common.archive

class ArchiveEntry(
val name: String,
val isFile: Boolean,
)
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tachiyomi.core.archive
package tachiyomi.core.common.archive

import android.content.Context
import android.os.ParcelFileDescriptor
Expand All @@ -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 <T> useEntries(block: (Sequence<Archive.Entry>) -> T): T =
Archive(address, size).use { block(generateSequence { it.readEntry() }) }
inline fun <T> useEntries(block: (Sequence<ArchiveEntry>) -> 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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tachiyomi.core.archive
package tachiyomi.core.common.archive

import android.content.Context
import android.system.Os
Expand Down

0 comments on commit 348cf3e

Please sign in to comment.