mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
添加Umd格式支持
This commit is contained in:
parent
9062fc5e65
commit
950afc36d7
Binary file not shown.
@ -67,6 +67,9 @@ data class Book(
|
||||
fun isEpub(): Boolean {
|
||||
return originName.endsWith(".epub", true)
|
||||
}
|
||||
fun isUmd(): Boolean {
|
||||
return originName.endsWith(".umd", true)
|
||||
}
|
||||
|
||||
fun isOnLineTxt(): Boolean {
|
||||
return !isLocalBook() && type == 0
|
||||
|
@ -190,7 +190,7 @@ object BookHelp {
|
||||
}
|
||||
|
||||
fun getContent(book: Book, bookChapter: BookChapter): String? {
|
||||
if (book.isLocalTxt()) {
|
||||
if (book.isLocalTxt()||book.isUmd()) {
|
||||
return LocalBook.getContext(book, bookChapter)
|
||||
} else if (book.isEpub() && !hasContent(book, bookChapter)) {
|
||||
val string = LocalBook.getContext(book, bookChapter)
|
||||
@ -203,7 +203,7 @@ object BookHelp {
|
||||
).writeText(it)
|
||||
}
|
||||
return string
|
||||
} else {
|
||||
}else {
|
||||
val file = FileUtils.getFile(
|
||||
downloadDir,
|
||||
cacheFolderName,
|
||||
|
@ -28,6 +28,8 @@ object LocalBook {
|
||||
fun getChapterList(book: Book): ArrayList<BookChapter> {
|
||||
return if (book.isEpub()) {
|
||||
EpubFile.getChapterList(book)
|
||||
}else if(book.isUmd()){
|
||||
UmdFile.getChapterList(book)
|
||||
} else {
|
||||
AnalyzeTxtFile().analyze(book)
|
||||
}
|
||||
@ -36,6 +38,8 @@ object LocalBook {
|
||||
fun getContext(book: Book, chapter: BookChapter): String? {
|
||||
return if (book.isEpub()) {
|
||||
EpubFile.getContent(book, chapter)
|
||||
}else if (book.isUmd()){
|
||||
UmdFile.getContent(book, chapter)
|
||||
} else {
|
||||
AnalyzeTxtFile.getContent(book, chapter)
|
||||
}
|
||||
@ -121,13 +125,14 @@ object LocalBook {
|
||||
)
|
||||
)
|
||||
if (book.isEpub()) EpubFile.upBookInfo(book)
|
||||
if (book.isUmd()) UmdFile.upBookInfo(book)
|
||||
appDb.bookDao.insert(book)
|
||||
return book
|
||||
}
|
||||
|
||||
fun deleteBook(book: Book, deleteOriginal: Boolean) {
|
||||
kotlin.runCatching {
|
||||
if (book.isLocalTxt()) {
|
||||
if (book.isLocalTxt()||book.isUmd()) {
|
||||
val bookFile = FileUtils.getFile(cacheFolder, book.originName)
|
||||
bookFile.delete()
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package io.legado.app.model.localBook
|
||||
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import io.legado.app.data.entities.Book
|
||||
import io.legado.app.data.entities.BookChapter
|
||||
|
||||
@ -21,12 +22,9 @@ class UmdFile(var book: Book) {
|
||||
|
||||
@Synchronized
|
||||
private fun getEFile(book: Book): UmdFile {
|
||||
//BookHelp.getEpubFile(book)
|
||||
|
||||
if (eFile == null || eFile?.book?.bookUrl != book.bookUrl) {
|
||||
eFile = UmdFile(book)
|
||||
//对于Epub文件默认不启用替换
|
||||
//book.setUseReplaceRule(false)
|
||||
return eFile!!
|
||||
}
|
||||
eFile?.book = book
|
||||
@ -112,7 +110,7 @@ class UmdFile(var book: Book) {
|
||||
}
|
||||
}
|
||||
private fun getContent(chapter: BookChapter): String? {
|
||||
return umdBook?.chapters?.getContentString(chapter.index)
|
||||
return umdBook?.chapters?.getContentString(chapter.index)
|
||||
}
|
||||
|
||||
private fun getChapterList(): ArrayList<BookChapter> {
|
||||
@ -124,8 +122,11 @@ class UmdFile(var book: Book) {
|
||||
chapter.index = index
|
||||
chapter.bookUrl = book.bookUrl
|
||||
chapter.url = index.toString();
|
||||
Log.d("UMD",chapter.url)
|
||||
chapterList.add(chapter)
|
||||
}
|
||||
book.latestChapterTitle = chapterList.lastOrNull()?.title
|
||||
book.totalChapterNum = chapterList.size
|
||||
return chapterList
|
||||
}
|
||||
|
||||
|
@ -197,6 +197,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
|
||||
} else if (!item.isDir
|
||||
&& !item.name.endsWith(".txt", true)
|
||||
&& !item.name.endsWith(".epub", true)
|
||||
&& !item.name.endsWith(".umd", true)
|
||||
) {
|
||||
docList.removeAt(i)
|
||||
}
|
||||
@ -226,6 +227,7 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
|
||||
)
|
||||
} else if (it.name.endsWith(".txt", true)
|
||||
|| it.name.endsWith(".epub", true)
|
||||
|| it.name.endsWith(".umd", true)
|
||||
) {
|
||||
docList.add(
|
||||
DocItem(
|
||||
|
@ -27,8 +27,8 @@ public class UmdChapters {
|
||||
return titles;
|
||||
}
|
||||
|
||||
private List<byte[]> titles = new ArrayList<byte[]>();
|
||||
public List<Integer> contentLengths = new ArrayList<Integer>();
|
||||
private List<byte[]> titles = new ArrayList<>();
|
||||
public List<Integer> contentLengths = new ArrayList<>();
|
||||
public ByteArrayOutputStream contents = new ByteArrayOutputStream();
|
||||
|
||||
public void addTitle(String s){
|
||||
@ -44,7 +44,7 @@ public class UmdChapters {
|
||||
return contentLengths.get(index);
|
||||
}
|
||||
|
||||
public byte[] getContent(int index) throws Exception {
|
||||
public byte[] getContent(int index) {
|
||||
int st=contentLengths.get(index);
|
||||
byte[] b=contents.toByteArray();
|
||||
int end=index+1<contentLengths.size()?contentLengths.get(index+1): getTotalContentLen();
|
||||
@ -56,16 +56,8 @@ public class UmdChapters {
|
||||
return bAr;
|
||||
|
||||
}
|
||||
public String getContentString(int index) throws Exception {
|
||||
int st=contentLengths.get(index);
|
||||
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 getContentString(int index) {
|
||||
return UmdUtils.unicodeBytesToString(getContent(index)).replace((char) 0x2029, '\n');
|
||||
|
||||
}
|
||||
public String getTitle(int index){
|
||||
|
@ -2,8 +2,7 @@ package me.ag2s.umdlib.umd;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import me.ag2s.umdlib.domain.UmdBook;
|
||||
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.UmdUtils;
|
||||
|
||||
/**
|
||||
* UMD格式的电子书解析
|
||||
* 格式规范参考:
|
||||
* http://blog.sina.com.cn/s/blog_7c8dc2d501018o5d.html
|
||||
* http://blog.sina.com.cn/s/blog_7c8dc2d501018o5l.html
|
||||
*
|
||||
*/
|
||||
|
||||
public class UmdReader {
|
||||
UmdBook book;
|
||||
InputStream inputStream;
|
||||
int[] _ChaptersOff;
|
||||
int _AdditionalCheckNumber;
|
||||
int _TotalContentLen;
|
||||
boolean end = false;
|
||||
List<byte[]> _ZippedContentList = new ArrayList<>();
|
||||
|
||||
// public UmdReader(InputStream inputStream) {
|
||||
// this.inputStream = inputStream;
|
||||
// }
|
||||
|
||||
// public UmdReader(File file) throws IOException {
|
||||
// this.inputStream = new FileInputStream(file);
|
||||
// }
|
||||
public synchronized UmdBook read(InputStream inputStream) throws Exception {
|
||||
|
||||
public UmdBook read(InputStream inputStream) throws Exception {
|
||||
book = new UmdBook();
|
||||
this.inputStream=inputStream;
|
||||
StreamReader reader = new StreamReader(inputStream);
|
||||
UmdHeader umdHeader = new UmdHeader();
|
||||
book.setHeader(umdHeader);
|
||||
@ -61,7 +61,7 @@ public class UmdReader {
|
||||
num1 = segType;
|
||||
|
||||
}
|
||||
//System.out.println(book.getHeader().toString());
|
||||
System.out.println(book.getHeader().toString());
|
||||
return book;
|
||||
|
||||
}
|
||||
@ -97,7 +97,6 @@ public class UmdReader {
|
||||
System.out.println(length);
|
||||
book.getChapters().contents.write(UmdUtils.decompress(reader.readBytes(length)));
|
||||
book.getChapters().contents.flush();
|
||||
//this._ZippedContentList.add(reader.readBytes(length));
|
||||
break;
|
||||
} else {
|
||||
for (int i = 0; i < book.getNum(); i++) {
|
||||
@ -214,10 +213,6 @@ public class UmdReader {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UmdReader{" +
|
||||
|
Loading…
Reference in New Issue
Block a user