Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Sep 20, 2023
1 parent a712007 commit e8dc579
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 389 deletions.
6 changes: 0 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,6 @@ dependencies {

//apache
implementation('org.apache.commons:commons-text:1.10.0')
//noinspection GradleDependency,1.23.0对安卓支持不好
implementation 'org.apache.commons:commons-compress:1.22'
implementation 'org.tukaani:xz:1.9'

//RAR
implementation 'com.github.junrar:junrar:7.5.5'

//MarkDown
def markwonVersion = "4.6.2"
Expand Down
32 changes: 5 additions & 27 deletions app/src/main/java/io/legado/app/help/JsExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.net.Uri
import androidx.annotation.Keep
import cn.hutool.core.codec.Base64
import cn.hutool.core.util.HexUtil
import com.github.junrar.Archive
import com.github.junrar.rarfile.FileHeader
import com.github.liuyueyi.quick.transfer.ChineseUtils
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppConst.dateFormat
Expand All @@ -23,13 +21,11 @@ import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.*
import io.legado.app.utils.compress.LibArchiveUtils
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import okio.use
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry
import org.apache.commons.compress.archivers.sevenz.SevenZFile
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel
import org.jsoup.Connection
import org.jsoup.Jsoup
import splitties.init.appCtx
Expand Down Expand Up @@ -699,18 +695,9 @@ interface JsExtensions : JsEncodeUtils {
HexUtil.decodeHex(url)
}

val bos = ByteArrayOutputStream()
Archive(ByteArrayInputStream(bytes)).use { archive ->
var entry: FileHeader
while (archive.nextFileHeader().also { entry = it } != null) {
if (entry.fileName.equals(path)) {
archive.getInputStream(entry).use { it.copyTo(bos) }
return bos.toByteArray()
}
}
return ByteArrayInputStream(bytes).use {
LibArchiveUtils.getByteArrayContent(it, path)
}
log("getRarContent 未发现内容")
return null
}

/**
Expand All @@ -726,18 +713,9 @@ interface JsExtensions : JsEncodeUtils {
HexUtil.decodeHex(url)
}

val bos = ByteArrayOutputStream()
SevenZFile(SeekableInMemoryByteChannel(bytes)).use { sevenZFile ->
var entry: SevenZArchiveEntry
while (sevenZFile.nextEntry.also { entry = it } != null) {
if (entry.name.equals(path)) {
sevenZFile.getInputStream(entry).use { it.copyTo(bos) }
return bos.toByteArray()
}
}
return ByteArrayInputStream(bytes).use {
LibArchiveUtils.getByteArrayContent(it, path)
}
log("get7zContent 未发现内容")
return null
}


Expand Down
51 changes: 43 additions & 8 deletions app/src/main/java/io/legado/app/utils/compress/LibArchiveUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.legado.app.lib.icu4j.CharsetDetector
import me.zhanghai.android.libarchive.Archive
import me.zhanghai.android.libarchive.ArchiveEntry
import me.zhanghai.android.libarchive.ArchiveException
import splitties.init.appCtx
import java.io.File
import java.io.FileDescriptor
import java.io.IOException
Expand All @@ -23,6 +24,7 @@ import java.nio.charset.StandardCharsets

object LibArchiveUtils {

val cachePath = File(appCtx.cacheDir, "archive")

@Throws(ArchiveException::class)
fun openArchive(
Expand Down Expand Up @@ -60,15 +62,12 @@ object LibArchiveUtils {
Archive.readOpen1(archive)
successful = true
return archive


} finally {
if (!successful) {
Archive.free(archive)
}
}


}


Expand Down Expand Up @@ -135,7 +134,6 @@ object LibArchiveUtils {
}
}


}


Expand Down Expand Up @@ -216,7 +214,7 @@ object LibArchiveUtils {
fun unArchive(
pfd: ParcelFileDescriptor,
destDir: File,
filter: ((String) -> Boolean)?
filter: ((String) -> Boolean)? = null
): List<File> {
return unArchive(openArchive(pfd), destDir, filter)
}
Expand Down Expand Up @@ -288,6 +286,46 @@ object LibArchiveUtils {
return getFilesName(openArchive(pfd), filter)
}

fun getByteArrayContent(inputStream: InputStream, path: String): ByteArray? {
val archive = openArchive(inputStream)
try {
var entry = Archive.readNextHeader(archive)
while (entry != 0L) {
val entryName =
getEntryString(ArchiveEntry.pathnameUtf8(entry), ArchiveEntry.pathname(entry))
?: continue

val entryStat = ArchiveEntry.stat(entry)

//判断是否是文件夹
if (S_ISDIR(entryStat.stMode)) {
entry = Archive.readNextHeader(archive)
continue
}

if (entryName == path) {
cachePath.mkdirs()
val entryFile = File(cachePath, entry.toString())
entryFile.delete()
entryFile.createNewFile()
entryFile.setReadable(true)
entryFile.setExecutable(true)
ParcelFileDescriptor.open(entryFile, ParcelFileDescriptor.MODE_WRITE_ONLY).use {
Archive.readDataIntoFd(archive, it.fd)
}
val bytes = entryFile.readBytes()
entryFile.delete()
return bytes
}

entry = Archive.readNextHeader(archive)

}
} finally {
Archive.free(archive)
}
return null
}

@Throws(SecurityException::class)
private fun getFilesName(
Expand Down Expand Up @@ -320,13 +358,11 @@ object LibArchiveUtils {

entry = Archive.readNextHeader(archive)


}
} finally {
Archive.free(archive)
}


return fileNames
}

Expand All @@ -344,5 +380,4 @@ object LibArchiveUtils {

}


}
129 changes: 0 additions & 129 deletions app/src/main/java/io/legado/app/utils/compress/RarUtils.kt

This file was deleted.

Loading

0 comments on commit e8dc579

Please sign in to comment.