添加Umd格式支持

This commit is contained in:
ag2s20150909 2021-07-02 15:03:53 +08:00
parent 9062fc5e65
commit 950afc36d7
8 changed files with 35 additions and 37 deletions

Binary file not shown.

View File

@ -67,6 +67,9 @@ data class Book(
fun isEpub(): Boolean { fun isEpub(): Boolean {
return originName.endsWith(".epub", true) return originName.endsWith(".epub", true)
} }
fun isUmd(): Boolean {
return originName.endsWith(".umd", true)
}
fun isOnLineTxt(): Boolean { fun isOnLineTxt(): Boolean {
return !isLocalBook() && type == 0 return !isLocalBook() && type == 0

View File

@ -190,7 +190,7 @@ object BookHelp {
} }
fun getContent(book: Book, bookChapter: BookChapter): String? { fun getContent(book: Book, bookChapter: BookChapter): String? {
if (book.isLocalTxt()) { if (book.isLocalTxt()||book.isUmd()) {
return LocalBook.getContext(book, bookChapter) return LocalBook.getContext(book, bookChapter)
} else if (book.isEpub() && !hasContent(book, bookChapter)) { } else if (book.isEpub() && !hasContent(book, bookChapter)) {
val string = LocalBook.getContext(book, bookChapter) val string = LocalBook.getContext(book, bookChapter)
@ -203,7 +203,7 @@ object BookHelp {
).writeText(it) ).writeText(it)
} }
return string return string
} else { }else {
val file = FileUtils.getFile( val file = FileUtils.getFile(
downloadDir, downloadDir,
cacheFolderName, cacheFolderName,

View File

@ -28,6 +28,8 @@ object LocalBook {
fun getChapterList(book: Book): ArrayList<BookChapter> { fun getChapterList(book: Book): ArrayList<BookChapter> {
return if (book.isEpub()) { return if (book.isEpub()) {
EpubFile.getChapterList(book) EpubFile.getChapterList(book)
}else if(book.isUmd()){
UmdFile.getChapterList(book)
} else { } else {
AnalyzeTxtFile().analyze(book) AnalyzeTxtFile().analyze(book)
} }
@ -36,6 +38,8 @@ object LocalBook {
fun getContext(book: Book, chapter: BookChapter): String? { fun getContext(book: Book, chapter: BookChapter): String? {
return if (book.isEpub()) { return if (book.isEpub()) {
EpubFile.getContent(book, chapter) EpubFile.getContent(book, chapter)
}else if (book.isUmd()){
UmdFile.getContent(book, chapter)
} else { } else {
AnalyzeTxtFile.getContent(book, chapter) AnalyzeTxtFile.getContent(book, chapter)
} }
@ -121,13 +125,14 @@ object LocalBook {
) )
) )
if (book.isEpub()) EpubFile.upBookInfo(book) if (book.isEpub()) EpubFile.upBookInfo(book)
if (book.isUmd()) UmdFile.upBookInfo(book)
appDb.bookDao.insert(book) appDb.bookDao.insert(book)
return book return book
} }
fun deleteBook(book: Book, deleteOriginal: Boolean) { fun deleteBook(book: Book, deleteOriginal: Boolean) {
kotlin.runCatching { kotlin.runCatching {
if (book.isLocalTxt()) { if (book.isLocalTxt()||book.isUmd()) {
val bookFile = FileUtils.getFile(cacheFolder, book.originName) val bookFile = FileUtils.getFile(cacheFolder, book.originName)
bookFile.delete() bookFile.delete()
} }

View File

@ -1,6 +1,7 @@
package io.legado.app.model.localBook package io.legado.app.model.localBook
import android.net.Uri import android.net.Uri
import android.util.Log
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
@ -21,12 +22,9 @@ class UmdFile(var book: Book) {
@Synchronized @Synchronized
private fun getEFile(book: Book): UmdFile { private fun getEFile(book: Book): UmdFile {
//BookHelp.getEpubFile(book)
if (eFile == null || eFile?.book?.bookUrl != book.bookUrl) { if (eFile == null || eFile?.book?.bookUrl != book.bookUrl) {
eFile = UmdFile(book) eFile = UmdFile(book)
//对于Epub文件默认不启用替换
//book.setUseReplaceRule(false)
return eFile!! return eFile!!
} }
eFile?.book = book eFile?.book = book
@ -112,7 +110,7 @@ class UmdFile(var book: Book) {
} }
} }
private fun getContent(chapter: BookChapter): String? { private fun getContent(chapter: BookChapter): String? {
return umdBook?.chapters?.getContentString(chapter.index) return umdBook?.chapters?.getContentString(chapter.index)
} }
private fun getChapterList(): ArrayList<BookChapter> { private fun getChapterList(): ArrayList<BookChapter> {
@ -124,8 +122,11 @@ class UmdFile(var book: Book) {
chapter.index = index chapter.index = index
chapter.bookUrl = book.bookUrl chapter.bookUrl = book.bookUrl
chapter.url = index.toString(); chapter.url = index.toString();
Log.d("UMD",chapter.url)
chapterList.add(chapter) chapterList.add(chapter)
} }
book.latestChapterTitle = chapterList.lastOrNull()?.title
book.totalChapterNum = chapterList.size
return chapterList return chapterList
} }

View File

@ -197,6 +197,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
} else if (!item.isDir } else if (!item.isDir
&& !item.name.endsWith(".txt", true) && !item.name.endsWith(".txt", true)
&& !item.name.endsWith(".epub", true) && !item.name.endsWith(".epub", true)
&& !item.name.endsWith(".umd", true)
) { ) {
docList.removeAt(i) docList.removeAt(i)
} }
@ -226,6 +227,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
) )
} else if (it.name.endsWith(".txt", true) } else if (it.name.endsWith(".txt", true)
|| it.name.endsWith(".epub", true) || it.name.endsWith(".epub", true)
|| it.name.endsWith(".umd", true)
) { ) {
docList.add( docList.add(
DocItem( DocItem(

View File

@ -27,8 +27,8 @@ public class UmdChapters {
return titles; return titles;
} }
private List<byte[]> titles = new ArrayList<byte[]>(); private List<byte[]> titles = new ArrayList<>();
public List<Integer> contentLengths = new ArrayList<Integer>(); public List<Integer> contentLengths = new ArrayList<>();
public ByteArrayOutputStream contents = new ByteArrayOutputStream(); public ByteArrayOutputStream contents = new ByteArrayOutputStream();
public void addTitle(String s){ public void addTitle(String s){
@ -44,7 +44,7 @@ public class UmdChapters {
return contentLengths.get(index); return contentLengths.get(index);
} }
public byte[] getContent(int index) throws Exception { public byte[] getContent(int index) {
int st=contentLengths.get(index); int st=contentLengths.get(index);
byte[] b=contents.toByteArray(); byte[] b=contents.toByteArray();
int end=index+1<contentLengths.size()?contentLengths.get(index+1): getTotalContentLen(); int end=index+1<contentLengths.size()?contentLengths.get(index+1): getTotalContentLen();
@ -56,16 +56,8 @@ public class UmdChapters {
return bAr; return bAr;
} }
public String getContentString(int index) throws Exception { public String getContentString(int index) {
int st=contentLengths.get(index); return UmdUtils.unicodeBytesToString(getContent(index)).replace((char) 0x2029, '\n');
byte[] b=contents.toByteArray();
int end=index+1<contentLengths.size()?contentLengths.get(index+1): getTotalContentLen();
System.out.println("总长度:"+contents.size());
System.out.println("起始值:"+st);
System.out.println("结束值:"+end);
byte[] bAr=new byte[end-st];
System.arraycopy(b,st,bAr,0,bAr.length);
return UmdUtils.unicodeBytesToString(bAr);
} }
public String getTitle(int index){ public String getTitle(int index){

View File

@ -2,8 +2,7 @@ package me.ag2s.umdlib.umd;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import me.ag2s.umdlib.domain.UmdBook; import me.ag2s.umdlib.domain.UmdBook;
import me.ag2s.umdlib.domain.UmdCover; import me.ag2s.umdlib.domain.UmdCover;
@ -11,25 +10,26 @@ import me.ag2s.umdlib.domain.UmdHeader;
import me.ag2s.umdlib.tool.StreamReader; import me.ag2s.umdlib.tool.StreamReader;
import me.ag2s.umdlib.tool.UmdUtils; import me.ag2s.umdlib.tool.UmdUtils;
/**
* UMD格式的电子书解析
* 格式规范参考
* http://blog.sina.com.cn/s/blog_7c8dc2d501018o5d.html
* http://blog.sina.com.cn/s/blog_7c8dc2d501018o5l.html
*
*/
public class UmdReader { public class UmdReader {
UmdBook book; UmdBook book;
InputStream inputStream; InputStream inputStream;
int[] _ChaptersOff;
int _AdditionalCheckNumber; int _AdditionalCheckNumber;
int _TotalContentLen; int _TotalContentLen;
boolean end = false; boolean end = false;
List<byte[]> _ZippedContentList = new ArrayList<>();
// public UmdReader(InputStream inputStream) {
// this.inputStream = inputStream;
// }
// public UmdReader(File file) throws IOException { public synchronized UmdBook read(InputStream inputStream) throws Exception {
// this.inputStream = new FileInputStream(file);
// }
public UmdBook read(InputStream inputStream) throws Exception {
book = new UmdBook(); book = new UmdBook();
this.inputStream=inputStream;
StreamReader reader = new StreamReader(inputStream); StreamReader reader = new StreamReader(inputStream);
UmdHeader umdHeader = new UmdHeader(); UmdHeader umdHeader = new UmdHeader();
book.setHeader(umdHeader); book.setHeader(umdHeader);
@ -61,7 +61,7 @@ public class UmdReader {
num1 = segType; num1 = segType;
} }
//System.out.println(book.getHeader().toString()); System.out.println(book.getHeader().toString());
return book; return book;
} }
@ -97,7 +97,6 @@ public class UmdReader {
System.out.println(length); System.out.println(length);
book.getChapters().contents.write(UmdUtils.decompress(reader.readBytes(length))); book.getChapters().contents.write(UmdUtils.decompress(reader.readBytes(length)));
book.getChapters().contents.flush(); book.getChapters().contents.flush();
//this._ZippedContentList.add(reader.readBytes(length));
break; break;
} else { } else {
for (int i = 0; i < book.getNum(); i++) { for (int i = 0; i < book.getNum(); i++) {
@ -214,10 +213,6 @@ public class UmdReader {
} }
@Override @Override
public String toString() { public String toString() {
return "UmdReader{" + return "UmdReader{" +