pull/399/head
Celeter 4 years ago
parent b6d2cf43f8
commit d13e8ad26d
  1. 12
      app/src/main/assets/updateLog.md
  2. 44
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 17
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  4. 27
      app/src/main/java/io/legado/app/utils/StringUtils.kt

@ -3,6 +3,18 @@
* 关注合作公众号 **[小说拾遗]()** 获取好看的小说。 * 关注合作公众号 **[小说拾遗]()** 获取好看的小说。
- 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 - 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。
**2020/09/29**
* 增加了几个方法用于处理文件
```
//文件下载,content为十六进制字符串,url用于生成文件名,返回文件路径
downloadFile(content: String, url: String): String
//文件解压,zipPath为压缩文件路径,返回解压路径
unzipFile(zipPath: String): String
//文件夹内所有文件读取
getTxtInFolder(unzipPath: String): String
```
* 增加type字段,返回16进制字符串,栗:`https://www.baidu.com,{"type":"zip"}`
**2020/09/24** **2020/09/24**
* 修复规则解析bug * 修复规则解析bug

@ -43,31 +43,57 @@ interface JsExtensions {
} }
/** /**
* js实现读取压缩文件内的文件内容并删除压缩文件 * js实现文件下载
*/ */
fun getTxtInZip(zipPath: String): String { fun downloadFile(content: String, url: String): String {
// 解压路径 val zipPath = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.zip"
)
FileUtils.deleteFile(zipPath)
val zipFile = FileUtils.createFileIfNotExist(zipPath)
StringUtils.hexStringToByte(content).let {
if (it != null) {
zipFile.writeBytes(it)
}
}
return zipPath
}
/**
* js实现压缩文件解压
*/
fun unzipFile(zipPath: String): String {
val unzipPath = FileUtils.getPath( val unzipPath = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
FileUtils.getNameExcludeExtension(zipPath) FileUtils.getNameExcludeExtension(zipPath)
) )
FileUtils.deleteFile(unzipPath) FileUtils.deleteFile(unzipPath)
val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath)
val zipFile = FileUtils.createFileIfNotExist(zipPath) val zipFile = FileUtils.createFileIfNotExist(zipPath)
val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath)
ZipUtils.unzipFile(zipFile, unzipFolder) ZipUtils.unzipFile(zipFile, unzipFolder)
val content = StringBuilder() FileUtils.deleteFile(zipPath)
return unzipPath
}
/**
* js实现文件夹内所有文件读取
*/
fun getTxtInFolder(unzipPath: String): String {
val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath)
val contents = StringBuilder()
unzipFolder.listFiles().let { unzipFolder.listFiles().let {
if (it != null) { if (it != null) {
for (f in it) { for (f in it) {
val charsetName = EncodingDetect.getEncode(f) val charsetName = EncodingDetect.getEncode(f)
content.append(String(f.readBytes(), Charset.forName(charsetName))).append("\n") contents.append(String(f.readBytes(), Charset.forName(charsetName)))
.append("\n")
} }
content.deleteCharAt(content.length - 1) contents.deleteCharAt(contents.length - 1)
} }
} }
FileUtils.deleteFile(zipPath)
FileUtils.deleteFile(unzipPath) FileUtils.deleteFile(unzipPath)
return content.toString() return contents.toString()
} }
/** /**

@ -328,21 +328,8 @@ class AnalyzeUrl(
jsStr: String? = null, jsStr: String? = null,
sourceRegex: String? = null, sourceRegex: String? = null,
): Res { ): Res {
if (type.equals("zip", true)) { if (type != null) {
val zipPath = FileUtils.getPath( return Res(url, StringUtils.byteToHexString(getResponseBytes(tag)))
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(tag)}.zip"
)
FileUtils.deleteFile(zipPath)
getResponseBytes(tag).let {
return if (it != null) {
FileUtils.createFileIfNotExist(zipPath).writeBytes(it)
// 返回压缩文件的下载路径
Res(url, zipPath)
} else {
Res(url, "文件下载失败\n${zipPath}")
}
}
} }
if (useWebView) { if (useWebView) {
val params = AjaxWebView.AjaxParams(url) val params = AjaxWebView.AjaxParams(url)

@ -45,6 +45,7 @@ object StringUtils {
//将时间转换成日期 //将时间转换成日期
fun dateConvert(time: Long, pattern: String): String { fun dateConvert(time: Long, pattern: String): String {
val date = Date(time) val date = Date(time)
@SuppressLint("SimpleDateFormat") @SuppressLint("SimpleDateFormat")
val format = SimpleDateFormat(pattern) val format = SimpleDateFormat(pattern)
return format.format(date) return format.format(date)
@ -292,4 +293,30 @@ object StringUtils {
return buf.toString() return buf.toString()
} }
fun byteToHexString(bytes: ByteArray?): String {
if (bytes == null) return ""
val sb = StringBuilder(bytes.size * 2)
for (b in bytes) {
val hex = 0xff and b.toInt()
if (hex < 16) {
sb.append('0')
}
sb.append(Integer.toHexString(hex)).append(" ")
}
return sb.toString()
}
fun hexStringToByte(hexString: String): ByteArray? {
val hexStr = hexString.replace(" ", "")
val len = hexStr.length
val bytes = ByteArray(len / 2)
var i = 0
while (i < len) {
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
bytes[i / 2] = ((Character.digit(hexString[i], 16) shl 4) +
Character.digit(hexString[i + 1], 16)).toByte()
i += 2
}
return bytes
}
} }

Loading…
Cancel
Save