Horis 2 лет назад
Родитель
Сommit
a350758c72

+ 34 - 26
app/src/main/java/io/legado/app/service/ExportBookService.kt

@@ -41,6 +41,7 @@ import io.legado.app.utils.activityPendingIntent
 import io.legado.app.utils.cnCompare
 import io.legado.app.utils.createFolderIfNotExist
 import io.legado.app.utils.isContentScheme
+import io.legado.app.utils.outputStream
 import io.legado.app.utils.postEvent
 import io.legado.app.utils.readBytes
 import io.legado.app.utils.readText
@@ -230,21 +231,24 @@ class ExportBookService : BaseService() {
         DocumentUtils.delete(doc, filename)
         val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename)
             ?: throw NoStackTraceException("创建文档失败,请尝试重新设置导出文件夹")
+        val charset = Charset.forName(AppConfig.exportCharset)
         contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs ->
-            getAllContents(book) { text, srcList ->
-                bookOs.write(text.toByteArray(Charset.forName(AppConfig.exportCharset)))
-                srcList?.forEach {
-                    val vFile = BookHelp.getImage(book, it.src)
-                    if (vFile.exists()) {
-                        DocumentUtils.createFileIfNotExist(
-                            doc,
-                            "${it.index}-${MD5Utils.md5Encode16(it.src)}.jpg",
-                            subDirs = arrayOf(
-                                "${book.name}_${book.author}",
-                                "images",
-                                it.chapterTitle
-                            )
-                        )?.writeBytes(this, vFile.readBytes())
+            BufferedOutputStream(bookOs, 64 * 1024).use { bos ->
+                getAllContents(book) { text, srcList ->
+                    bos.write(text.toByteArray(charset))
+                    srcList?.forEach {
+                        val vFile = BookHelp.getImage(book, it.src)
+                        if (vFile.exists()) {
+                            DocumentUtils.createFileIfNotExist(
+                                doc,
+                                "${it.index}-${MD5Utils.md5Encode16(it.src)}.jpg",
+                                subDirs = arrayOf(
+                                    "${book.name}_${book.author}",
+                                    "images",
+                                    it.chapterTitle
+                                )
+                            )?.writeBytes(this, vFile.readBytes())
+                        }
                     }
                 }
             }
@@ -259,18 +263,22 @@ class ExportBookService : BaseService() {
         val filename = book.getExportFileName("txt")
         val bookPath = FileUtils.getPath(file, filename)
         val bookFile = FileUtils.createFileWithReplace(bookPath)
-        getAllContents(book) { text, srcList ->
-            bookFile.appendText(text, Charset.forName(AppConfig.exportCharset))
-            srcList?.forEach {
-                val vFile = BookHelp.getImage(book, it.src)
-                if (vFile.exists()) {
-                    FileUtils.createFileIfNotExist(
-                        file,
-                        "${book.name}_${book.author}",
-                        "images",
-                        it.chapterTitle,
-                        "${it.index}-${MD5Utils.md5Encode16(it.src)}.jpg"
-                    ).writeBytes(vFile.readBytes())
+        val charset = Charset.forName(AppConfig.exportCharset)
+        val bos = BufferedOutputStream(bookFile.outputStream(true), 64 * 1024)
+        bos.use {
+            getAllContents(book) { text, srcList ->
+                bos.write(text.toByteArray(charset))
+                srcList?.forEach {
+                    val vFile = BookHelp.getImage(book, it.src)
+                    if (vFile.exists()) {
+                        FileUtils.createFileIfNotExist(
+                            file,
+                            "${book.name}_${book.author}",
+                            "images",
+                            it.chapterTitle,
+                            "${it.index}-${MD5Utils.md5Encode16(it.src)}.jpg"
+                        ).writeBytes(vFile.readBytes())
+                    }
                 }
             }
         }

+ 5 - 0
app/src/main/java/io/legado/app/utils/FileExtensions.kt

@@ -4,6 +4,7 @@ package io.legado.app.utils
 
 import android.net.Uri
 import java.io.File
+import java.io.FileOutputStream
 
 fun File.getFile(vararg subDirFiles: String): File {
     val path = FileUtils.getPath(this, *subDirFiles)
@@ -79,3 +80,7 @@ fun File.checkWrite(): Boolean {
         false
     }
 }
+
+fun File.outputStream(append: Boolean = false): FileOutputStream {
+    return FileOutputStream(this, append)
+}