From bb409e5ced5c0a9851ad47d6d65017b42e498807 Mon Sep 17 00:00:00 2001 From: Jobobby04 Date: Sat, 23 Oct 2021 14:04:33 -0400 Subject: [PATCH] Workaround for cbz long filename error --- .../tachiyomi/data/download/Downloader.kt | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index 1945a1d43..8bb4cfc0c 100755 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -484,35 +484,40 @@ class Downloader( // Only rename the directory if it's downloaded. if (download.status == Download.State.DOWNLOADED) { - if (preferences.saveChaptersAsCBZ().get()) { - val zip = mangaDir.createFile("$dirname.cbz.tmp") - val zipOut = ZipOutputStream(BufferedOutputStream(zip.openOutputStream())) - val compressionLevel = preferences.saveChaptersAsCBZLevel().get() + var zip: UniFile? = null + if ( + preferences.saveChaptersAsCBZ().get() && + mangaDir.createFile("$dirname.cbz.tmp").also { zip = it } != null + ) { + ZipOutputStream(zip!!.openOutputStream().buffered()).use { zipOut -> + val compressionLevel = preferences.saveChaptersAsCBZLevel().get() - zipOut.setLevel(compressionLevel) + zipOut.setLevel(compressionLevel) - if (compressionLevel == 0) { - zipOut.setMethod(ZipEntry.STORED) - } - - tmpDir.listFiles()?.forEach { img -> - val input = img.openInputStream() - val data = input.readBytes() - val entry = ZipEntry(img.name) if (compressionLevel == 0) { - val crc = CRC32() - val size = img.length() - crc.update(data) - entry.crc = crc.value - entry.compressedSize = size - entry.size = size + zipOut.setMethod(ZipEntry.STORED) + } + + tmpDir.listFiles()?.forEach { img -> + img.openInputStream().use { input -> + val data = input.readBytes() + val entry = ZipEntry(img.name) + if (compressionLevel == 0) { + val crc = CRC32() + val size = img.length() + crc.update(data) + entry.crc = crc.value + entry.compressedSize = size + entry.size = size + } + zipOut.putNextEntry(entry) + zipOut.write(data) + zipOut.closeEntry() + } } - zipOut.putNextEntry(entry) - zipOut.write(data) - input.close() } - zipOut.close() - zip.renameTo("$dirname.cbz") + + zip!!.renameTo("$dirname.cbz") tmpDir.delete() } else { tmpDir.renameTo(dirname)