|
|
|
@ -1,8 +1,7 @@
|
|
|
|
package org.koitharu.kotatsu.local.data
|
|
|
|
package org.koitharu.kotatsu.local.data
|
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.CheckResult
|
|
|
|
import androidx.annotation.CheckResult
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
|
|
|
import java.io.File
|
|
|
|
import java.io.File
|
|
|
|
import java.io.FileInputStream
|
|
|
|
import java.io.FileInputStream
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
@ -27,12 +26,14 @@ class WritableCbzFile(private val file: File) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ZipInputStream(FileInputStream(file)).use { zip ->
|
|
|
|
ZipInputStream(FileInputStream(file)).use { zip ->
|
|
|
|
var entry = zip.nextEntry
|
|
|
|
var entry = zip.nextEntry
|
|
|
|
while (entry != null) {
|
|
|
|
while (entry != null && currentCoroutineContext().isActive) {
|
|
|
|
val target = File(dir.path + File.separator + entry.name)
|
|
|
|
val target = File(dir.path + File.separator + entry.name)
|
|
|
|
|
|
|
|
runInterruptible {
|
|
|
|
target.parentFile?.mkdirs()
|
|
|
|
target.parentFile?.mkdirs()
|
|
|
|
target.outputStream().use { out ->
|
|
|
|
target.outputStream().use { out ->
|
|
|
|
zip.copyTo(out)
|
|
|
|
zip.copyTo(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
zip.closeEntry()
|
|
|
|
zip.closeEntry()
|
|
|
|
entry = zip.nextEntry
|
|
|
|
entry = zip.nextEntry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -51,12 +52,14 @@ class WritableCbzFile(private val file: File) {
|
|
|
|
tempFile.delete()
|
|
|
|
tempFile.delete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
runInterruptible {
|
|
|
|
ZipOutputStream(FileOutputStream(tempFile)).use { zip ->
|
|
|
|
ZipOutputStream(FileOutputStream(tempFile)).use { zip ->
|
|
|
|
dir.listFiles()?.forEach {
|
|
|
|
dir.listFiles()?.forEach {
|
|
|
|
zipFile(it, it.name, zip)
|
|
|
|
zipFile(it, it.name, zip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zip.flush()
|
|
|
|
zip.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
tempFile.renameTo(file)
|
|
|
|
tempFile.renameTo(file)
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (tempFile.exists()) {
|
|
|
|
if (tempFile.exists()) {
|
|
|
|
@ -67,12 +70,10 @@ class WritableCbzFile(private val file: File) {
|
|
|
|
|
|
|
|
|
|
|
|
operator fun get(name: String) = File(dir, name)
|
|
|
|
operator fun get(name: String) = File(dir, name)
|
|
|
|
|
|
|
|
|
|
|
|
operator fun set(name: String, file: File) {
|
|
|
|
suspend fun put(name: String, file: File) = runInterruptible(Dispatchers.IO) {
|
|
|
|
file.copyTo(this[name], overwrite = true)
|
|
|
|
file.copyTo(this[name], overwrite = true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun zipFile(fileToZip: File, fileName: String, zipOut: ZipOutputStream) {
|
|
|
|
private fun zipFile(fileToZip: File, fileName: String, zipOut: ZipOutputStream) {
|
|
|
|
if (fileToZip.isDirectory) {
|
|
|
|
if (fileToZip.isDirectory) {
|
|
|
|
if (fileName.endsWith("/")) {
|
|
|
|
if (fileName.endsWith("/")) {
|
|
|
|
@ -92,5 +93,4 @@ class WritableCbzFile(private val file: File) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|