This commit is contained in:
Horis 2023-11-29 11:19:23 +08:00
parent 301d70c081
commit 6cd087be87
2 changed files with 39 additions and 26 deletions

View File

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

View File

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