|
|
|
@ -6,12 +6,10 @@ import android.net.Uri |
|
|
|
|
import android.text.TextUtils |
|
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
|
import io.legado.app.utils.* |
|
|
|
|
import me.ag2s.epublib.domain.EpubBook |
|
|
|
|
import me.ag2s.epublib.domain.MediaTypes |
|
|
|
|
import me.ag2s.epublib.domain.Resources |
|
|
|
|
import me.ag2s.epublib.epub.EpubReader |
|
|
|
|
import me.ag2s.epublib.util.ResourceUtil |
|
|
|
|
import org.jsoup.Jsoup |
|
|
|
|
import splitties.init.appCtx |
|
|
|
|
import java.io.File |
|
|
|
@ -20,8 +18,7 @@ import java.io.IOException |
|
|
|
|
import java.io.InputStream |
|
|
|
|
import java.nio.charset.Charset |
|
|
|
|
import java.util.* |
|
|
|
|
import java.util.zip.ZipEntry |
|
|
|
|
import java.util.zip.ZipInputStream |
|
|
|
|
import java.util.zip.ZipFile |
|
|
|
|
|
|
|
|
|
class EpubFile(var book: Book) { |
|
|
|
|
|
|
|
|
@ -101,32 +98,46 @@ class EpubFile(var book: Book) { |
|
|
|
|
/*重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误*/ |
|
|
|
|
private fun readEpub(): EpubBook? { |
|
|
|
|
try { |
|
|
|
|
val input = if (book.bookUrl.isContentScheme()) { |
|
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
|
appCtx.contentResolver.openInputStream(uri) |
|
|
|
|
} else { |
|
|
|
|
File(book.bookUrl).inputStream() |
|
|
|
|
} |
|
|
|
|
input ?: return null |
|
|
|
|
val inZip = ZipInputStream(input) |
|
|
|
|
var zipEntry: ZipEntry? |
|
|
|
|
val resources = Resources() |
|
|
|
|
do { |
|
|
|
|
zipEntry = inZip.nextEntry |
|
|
|
|
if ((zipEntry == null) || zipEntry.isDirectory || zipEntry == ZipEntry("<error>")) continue |
|
|
|
|
val resource = ResourceUtil.createResource(zipEntry, inZip) |
|
|
|
|
if (resource.mediaType == MediaTypes.XHTML) resource.inputEncoding = "UTF-8" |
|
|
|
|
if (zipEntry.name.endsWith(".opf")) { |
|
|
|
|
/*掌上书苑有很多自制书OPF的nameSpace格式不标准,强制修复成正确的格式*/ |
|
|
|
|
val newS = String(resource.data).replace( |
|
|
|
|
"\\smlns=\"http://www.idpf.org/2007/opf\"".toRegex(), |
|
|
|
|
" xmlns=\"http://www.idpf.org/2007/opf\"" |
|
|
|
|
) |
|
|
|
|
resource.data = newS.toByteArray() |
|
|
|
|
//f |
|
|
|
|
val file = FileUtils.getFile( |
|
|
|
|
BookHelp.downloadDir, |
|
|
|
|
BookHelp.cacheFolderName, |
|
|
|
|
book.getFolderName(), |
|
|
|
|
"index.epubx" |
|
|
|
|
) |
|
|
|
|
if (!file.exists()) { |
|
|
|
|
val input = if (book.bookUrl.isContentScheme()) { |
|
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
|
appCtx.contentResolver.openInputStream(uri) |
|
|
|
|
} else { |
|
|
|
|
File(book.bookUrl).inputStream() |
|
|
|
|
} |
|
|
|
|
resources.add(resource) |
|
|
|
|
} while (zipEntry != null) |
|
|
|
|
if (resources.size() > 0) return EpubReader().readEpub(resources) |
|
|
|
|
input ?: return null |
|
|
|
|
FileUtils.writeInputStream(file, input) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
return EpubReader().readEpubLazy(ZipFile(file),"utf-8") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// val inZip = ZipInputStream(input) |
|
|
|
|
// var zipEntry: ZipEntry? |
|
|
|
|
// val resources = Resources() |
|
|
|
|
// do { |
|
|
|
|
// zipEntry = inZip.nextEntry |
|
|
|
|
// if ((zipEntry == null) || zipEntry.isDirectory || zipEntry == ZipEntry("<error>")) continue |
|
|
|
|
// val resource = ResourceUtil.createResource(zipEntry, inZip) |
|
|
|
|
// if (resource.mediaType == MediaTypes.XHTML) resource.inputEncoding = "UTF-8" |
|
|
|
|
// if (zipEntry.name.endsWith(".opf")) { |
|
|
|
|
// /*掌上书苑有很多自制书OPF的nameSpace格式不标准,强制修复成正确的格式*/ |
|
|
|
|
// val newS = String(resource.data).replace( |
|
|
|
|
// "\\smlns=\"http://www.idpf.org/2007/opf\"".toRegex(), |
|
|
|
|
// " xmlns=\"http://www.idpf.org/2007/opf\"" |
|
|
|
|
// ) |
|
|
|
|
// resource.data = newS.toByteArray() |
|
|
|
|
// } |
|
|
|
|
// resources.add(resource) |
|
|
|
|
// } while (zipEntry != null) |
|
|
|
|
// if (resources.size() > 0) return EpubReader().readEpub(resources) |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
e.printStackTrace() |
|
|
|
|
} |
|
|
|
|