|  | @@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.data.database.models.Chapter
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.data.database.models.Manga
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.data.download.model.Download
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.data.download.model.DownloadQueue
 | 
	
		
			
				|  |  | +import eu.kanade.tachiyomi.data.preference.PreferencesHelper
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.source.SourceManager
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.source.UnmeteredSource
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.source.model.Page
 | 
	
	
		
			
				|  | @@ -35,7 +36,11 @@ import rx.android.schedulers.AndroidSchedulers
 | 
	
		
			
				|  |  |  import rx.schedulers.Schedulers
 | 
	
		
			
				|  |  |  import rx.subscriptions.CompositeSubscription
 | 
	
		
			
				|  |  |  import uy.kohesive.injekt.injectLazy
 | 
	
		
			
				|  |  | +import java.io.BufferedOutputStream
 | 
	
		
			
				|  |  |  import java.io.File
 | 
	
		
			
				|  |  | +import java.util.zip.CRC32
 | 
	
		
			
				|  |  | +import java.util.zip.ZipEntry
 | 
	
		
			
				|  |  | +import java.util.zip.ZipOutputStream
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * This class is the one in charge of downloading chapters.
 | 
	
	
		
			
				|  | @@ -60,6 +65,8 @@ class Downloader(
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private val chapterCache: ChapterCache by injectLazy()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    private val preferences: PreferencesHelper by injectLazy()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Store for persisting downloads across restarts.
 | 
	
		
			
				|  |  |       */
 | 
	
	
		
			
				|  | @@ -484,13 +491,51 @@ class Downloader(
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // Only rename the directory if it's downloaded.
 | 
	
		
			
				|  |  |          if (download.status == Download.State.DOWNLOADED) {
 | 
	
		
			
				|  |  | -            tmpDir.renameTo(dirname)
 | 
	
		
			
				|  |  | +            if (preferences.saveChaptersAsCBZ().get()) {
 | 
	
		
			
				|  |  | +                archiveChapter(mangaDir, dirname, tmpDir)
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                tmpDir.renameTo(dirname)
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |              cache.addChapter(dirname, mangaDir, download.manga)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              DiskUtil.createNoMediaFile(tmpDir, context)
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * Archive the chapter pages as a CBZ.
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private fun archiveChapter(
 | 
	
		
			
				|  |  | +        mangaDir: UniFile,
 | 
	
		
			
				|  |  | +        dirname: String,
 | 
	
		
			
				|  |  | +        tmpDir: UniFile,
 | 
	
		
			
				|  |  | +    ) {
 | 
	
		
			
				|  |  | +        val zip = mangaDir.createFile("$dirname.cbz.tmp")
 | 
	
		
			
				|  |  | +        ZipOutputStream(BufferedOutputStream(zip.openOutputStream())).use { zipOut ->
 | 
	
		
			
				|  |  | +            zipOut.setMethod(ZipEntry.STORED)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            tmpDir.listFiles()?.forEach { img ->
 | 
	
		
			
				|  |  | +                img.openInputStream().use { input ->
 | 
	
		
			
				|  |  | +                    val data = input.readBytes()
 | 
	
		
			
				|  |  | +                    val size = img.length()
 | 
	
		
			
				|  |  | +                    val entry = ZipEntry(img.name).apply {
 | 
	
		
			
				|  |  | +                        val crc = CRC32().apply {
 | 
	
		
			
				|  |  | +                            update(data)
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        setCrc(crc.value)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                        compressedSize = size
 | 
	
		
			
				|  |  | +                        setSize(size)
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    zipOut.putNextEntry(entry)
 | 
	
		
			
				|  |  | +                    zipOut.write(data)
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        zip.renameTo("$dirname.cbz")
 | 
	
		
			
				|  |  | +        tmpDir.delete()
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * Completes a download. This method is called in the main thread.
 | 
	
		
			
				|  |  |       */
 |