pull/84/head
kunfei 5 years ago
parent 4c31435812
commit 9e8eed355c
  1. 2
      app/src/main/java/io/legado/app/data/entities/Book.kt
  2. 27
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  3. 7
      app/src/main/java/io/legado/app/ui/importbook/ImportBookViewModel.kt
  4. 8
      app/src/main/java/io/legado/app/utils/EncodingDetect.java

@ -19,7 +19,7 @@ data class Book(
var bookUrl: String = "", // 详情页Url(本地书源存储完整文件路径)
var tocUrl: String = "", // 目录页Url (toc=table of Contents)
var origin: String = BookType.local, // 书源URL(默认BookType.local)
var originName: String = "", //书源名称
var originName: String = "", //书源名称 or 本地书籍文件名
var name: String = "", // 书籍名称(书源获取)
var author: String = "", // 作者名称(书源获取)
override var kind: String? = null, // 分类信息(书源获取)

@ -2,20 +2,33 @@ package io.legado.app.model.localBook
import android.content.Context
import android.net.Uri
import io.legado.app.App
import io.legado.app.data.entities.Book
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.EncodingDetect
import io.legado.app.utils.FileUtils
import java.io.File
object AnalyzeTxtFile {
private const val folderName = "bookTxt"
private val cacheFolder: File by lazy {
val rootFile = App.INSTANCE.getExternalFilesDir(null)
?: App.INSTANCE.externalCacheDir
?: App.INSTANCE.cacheDir
FileUtils.createFileIfNotExist(rootFile, subDirs = *arrayOf(folderName))
}
fun analyze(context: Context, book: Book) {
context.contentResolver.openInputStream(Uri.parse(book.bookUrl))?.use { stream ->
val rawByteArray = ByteArray(2000)
stream.read(rawByteArray)
book.charset = EncodingDetect.getJavaEncode(rawByteArray)
val uri = Uri.parse(book.bookUrl)
val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = *arrayOf())
if (!bookFile.exists()) {
bookFile.createNewFile()
DocumentUtils.readBytes(context, uri)?.let {
bookFile.writeBytes(it)
}
}
book.charset = EncodingDetect.getEncode(bookFile)
}
}

@ -23,7 +23,12 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
if (smhStart != -1 && smhEnd != -1) {
name = (name.substring(smhStart + 1, smhEnd))
}
val book = Book(bookUrl = uriStr, name = name, author = author)
val book = Book(
bookUrl = uriStr,
name = name,
author = author,
originName = fileName
)
App.db.bookDao().insert(book)
}
}

@ -62,10 +62,10 @@ public class EncodingDetect {
}
} catch (Exception ignored) {
}
return getJavaEncode(bytes);
return getEncode(bytes);
}
public static String getJavaEncode(@NonNull byte[] bytes) {
public static String getEncode(@NonNull byte[] bytes) {
int len = bytes.length > 2000 ? 2000 : bytes.length;
byte[] cBytes = new byte[len];
System.arraycopy(bytes, 0, cBytes, 0, len);
@ -83,7 +83,7 @@ public class EncodingDetect {
/**
* 得到文件的编码
*/
public static String getJavaEncode(@NonNull String filePath) {
public static String getEncode(@NonNull String filePath) {
BytesEncodingDetect s = new BytesEncodingDetect();
String fileCode = BytesEncodingDetect.javaname[s
.detectEncoding(new File(filePath))];
@ -102,7 +102,7 @@ public class EncodingDetect {
/**
* 得到文件的编码
*/
public static String getJavaEncode(@NonNull File file) {
public static String getEncode(@NonNull File file) {
BytesEncodingDetect s = new BytesEncodingDetect();
String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(file)];
// UTF-16LE 特殊处理

Loading…
Cancel
Save