获取zip文件自动判断编码

pull/1155/head
gedoor 3 years ago
parent 09913c03e4
commit b9967c52ba
  1. 27
      app/src/main/java/io/legado/app/help/JsExtensions.kt

@ -19,6 +19,7 @@ import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
import java.net.URLEncoder import java.net.URLEncoder
import java.nio.charset.Charset
import java.util.* import java.util.*
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream import java.util.zip.ZipInputStream
@ -319,28 +320,14 @@ interface JsExtensions {
* @return zip指定文件的数据 * @return zip指定文件的数据
*/ */
fun getZipStringContent(url: String, path: String): String { fun getZipStringContent(url: String, path: String): String {
val bytes = if (url.startsWith("http://") || url.startsWith("https://")) { val byteArray = getZipByteArrayContent(url, path) ?: return ""
runBlocking { val charsetName = EncodingDetect.getEncode(byteArray)
return@runBlocking okHttpClient.newCall { url(url) }.bytes() return String(byteArray, Charset.forName(charsetName))
}
} else {
StringUtils.hexStringToByte(url)
} }
val bos = ByteArrayOutputStream()
val zis = ZipInputStream(ByteArrayInputStream(bytes))
var entry: ZipEntry? = zis.nextEntry
while (entry != null) { fun getZipStringContent(url: String, path: String, charsetName: String): String {
if (entry.name.equals(path)) { val byteArray = getZipByteArrayContent(url, path) ?: return ""
zis.use { it.copyTo(bos) } return String(byteArray, Charset.forName(charsetName))
return bos.toString()
}
entry = zis.nextEntry
}
Debug.log("getZipContent 未发现内容")
return ""
} }
/** /**

Loading…
Cancel
Save