diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 61e8f3a7d..e6db29155 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -3,6 +3,18 @@ * 关注合作公众号 **[小说拾遗]()** 获取好看的小说。 - 旧版数据导入教程:先在旧版阅读(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** * 修复规则解析bug diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 4d54afab4..fc63fb8ab 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -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( FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.getNameExcludeExtension(zipPath) ) FileUtils.deleteFile(unzipPath) - val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath) val zipFile = FileUtils.createFileIfNotExist(zipPath) + val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath) 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 { if (it != null) { for (f in it) { 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) - return content.toString() + return contents.toString() } /** diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 943abbd9b..f653c8032 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -328,21 +328,8 @@ class AnalyzeUrl( jsStr: String? = null, sourceRegex: String? = null, ): Res { - if (type.equals("zip", true)) { - val zipPath = FileUtils.getPath( - 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 (type != null) { + return Res(url, StringUtils.byteToHexString(getResponseBytes(tag))) } if (useWebView) { val params = AjaxWebView.AjaxParams(url) diff --git a/app/src/main/java/io/legado/app/utils/StringUtils.kt b/app/src/main/java/io/legado/app/utils/StringUtils.kt index e5b658c21..523281642 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -45,6 +45,7 @@ object StringUtils { //将时间转换成日期 fun dateConvert(time: Long, pattern: String): String { val date = Date(time) + @SuppressLint("SimpleDateFormat") val format = SimpleDateFormat(pattern) return format.format(date) @@ -292,4 +293,30 @@ object StringUtils { 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 + } }