Try Apache implementation of ZipFile instead

Docs: https://commons.apache.org/proper/commons-compress/
Related StackOverflow post: https://stackoverflow.com/a/54236244/4421500

Related to #9438
This commit is contained in:
arkon 2023-05-03 14:00:33 -04:00
parent 6ce42dc167
commit c623258e8c
3 changed files with 19 additions and 24 deletions

View file

@ -210,6 +210,7 @@ dependencies {
// Disk // Disk
implementation(libs.disklrucache) implementation(libs.disklrucache)
implementation(libs.unifile) implementation(libs.unifile)
implementation(libs.compress)
implementation(libs.junrar) implementation(libs.junrar)
// Preferences // Preferences

View file

@ -1,12 +1,13 @@
package eu.kanade.tachiyomi.ui.reader.loader package eu.kanade.tachiyomi.ui.reader.loader
import android.app.Application import android.app.Application
import android.os.Build
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import org.apache.commons.compress.archivers.zip.ZipFile
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.util.zip.ZipInputStream
/** /**
* Loader used to load a chapter from a .zip or .cbz file. * Loader used to load a chapter from a .zip or .cbz file.
@ -20,29 +21,21 @@ internal class ZipPageLoader(file: File) : PageLoader() {
} }
init { init {
ZipInputStream(FileInputStream(file)).use { zipInputStream -> ByteArrayOutputStream().use { byteArrayOutputStream ->
generateSequence { zipInputStream.nextEntry } FileInputStream(file).use { it.copyTo(byteArrayOutputStream) }
.filterNot { it.isDirectory }
.forEach { entry -> ZipFile(SeekableInMemoryByteChannel(byteArrayOutputStream.toByteArray())).use { zip ->
File(tmpDir, entry.name.substringAfterLast("/")) zip.entries.asSequence()
.also { it.createNewFile() } .filterNot { it.isDirectory }
.outputStream().use { pageOutputStream -> .forEach { entry ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { File(tmpDir, entry.name.substringAfterLast("/"))
pageOutputStream.write(zipInputStream.readNBytes(entry.size.toInt())) .also { it.createNewFile() }
} else { .outputStream().use { pageOutputStream ->
val buffer = ByteArray(2048) zip.getInputStream(entry).copyTo(pageOutputStream)
var len: Int pageOutputStream.flush()
while (
zipInputStream.read(buffer, 0, buffer.size)
.also { len = it } >= 0
) {
pageOutputStream.write(buffer, 0, len)
}
} }
pageOutputStream.flush() }
} }
zipInputStream.closeEntry()
}
} }
} }

View file

@ -32,6 +32,7 @@ jsoup = "org.jsoup:jsoup:1.15.4"
disklrucache = "com.jakewharton:disklrucache:2.0.2" disklrucache = "com.jakewharton:disklrucache:2.0.2"
unifile = "com.github.tachiyomiorg:unifile:17bec43" unifile = "com.github.tachiyomiorg:unifile:17bec43"
compress = "org.apache.commons:commons-compress:1.23.0"
junrar = "com.github.junrar:junrar:7.5.4" junrar = "com.github.junrar:junrar:7.5.4"
sqlite-framework = { module = "androidx.sqlite:sqlite-framework", version.ref = "sqlite" } sqlite-framework = { module = "androidx.sqlite:sqlite-framework", version.ref = "sqlite" }