|
|
@ -6,12 +6,10 @@ import android.net.Uri |
|
|
|
import android.text.TextUtils |
|
|
|
import android.text.TextUtils |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
|
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.utils.* |
|
|
|
import io.legado.app.utils.* |
|
|
|
import me.ag2s.epublib.domain.EpubBook |
|
|
|
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.epub.EpubReader |
|
|
|
import me.ag2s.epublib.util.ResourceUtil |
|
|
|
|
|
|
|
import org.jsoup.Jsoup |
|
|
|
import org.jsoup.Jsoup |
|
|
|
import splitties.init.appCtx |
|
|
|
import splitties.init.appCtx |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
@ -20,8 +18,7 @@ import java.io.IOException |
|
|
|
import java.io.InputStream |
|
|
|
import java.io.InputStream |
|
|
|
import java.nio.charset.Charset |
|
|
|
import java.nio.charset.Charset |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
import java.util.zip.ZipEntry |
|
|
|
import java.util.zip.ZipFile |
|
|
|
import java.util.zip.ZipInputStream |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EpubFile(var book: Book) { |
|
|
|
class EpubFile(var book: Book) { |
|
|
|
|
|
|
|
|
|
|
@ -101,32 +98,46 @@ class EpubFile(var book: Book) { |
|
|
|
/*重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误*/ |
|
|
|
/*重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误*/ |
|
|
|
private fun readEpub(): EpubBook? { |
|
|
|
private fun readEpub(): EpubBook? { |
|
|
|
try { |
|
|
|
try { |
|
|
|
val input = if (book.bookUrl.isContentScheme()) { |
|
|
|
//f |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
val file = FileUtils.getFile( |
|
|
|
appCtx.contentResolver.openInputStream(uri) |
|
|
|
BookHelp.downloadDir, |
|
|
|
} else { |
|
|
|
BookHelp.cacheFolderName, |
|
|
|
File(book.bookUrl).inputStream() |
|
|
|
book.getFolderName(), |
|
|
|
} |
|
|
|
"index.epubx" |
|
|
|
input ?: return null |
|
|
|
) |
|
|
|
val inZip = ZipInputStream(input) |
|
|
|
if (!file.exists()) { |
|
|
|
var zipEntry: ZipEntry? |
|
|
|
val input = if (book.bookUrl.isContentScheme()) { |
|
|
|
val resources = Resources() |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
do { |
|
|
|
appCtx.contentResolver.openInputStream(uri) |
|
|
|
zipEntry = inZip.nextEntry |
|
|
|
} else { |
|
|
|
if ((zipEntry == null) || zipEntry.isDirectory || zipEntry == ZipEntry("<error>")) continue |
|
|
|
File(book.bookUrl).inputStream() |
|
|
|
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) |
|
|
|
input ?: return null |
|
|
|
} while (zipEntry != null) |
|
|
|
FileUtils.writeInputStream(file, input) |
|
|
|
if (resources.size() > 0) return EpubReader().readEpub(resources) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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) { |
|
|
|
} catch (e: Exception) { |
|
|
|
e.printStackTrace() |
|
|
|
e.printStackTrace() |
|
|
|
} |
|
|
|
} |
|
|
|