|
|
@@ -13,6 +13,7 @@ import me.zhanghai.android.libarchive.ArchiveException
|
|
|
import java.io.File
|
|
|
import java.io.FileDescriptor
|
|
|
import java.io.IOException
|
|
|
+import java.io.InputStream
|
|
|
import java.io.InterruptedIOException
|
|
|
import java.nio.ByteBuffer
|
|
|
import java.nio.channels.SeekableByteChannel
|
|
|
@@ -23,52 +24,52 @@ import java.nio.charset.StandardCharsets
|
|
|
object LibArchiveUtils {
|
|
|
|
|
|
|
|
|
-// @Throws(ArchiveException::class)
|
|
|
-// fun openArchive(
|
|
|
-// inputStream: InputStream,
|
|
|
-// ): Long {
|
|
|
-// val archive: Long = Archive.readNew()
|
|
|
-// var successful = false
|
|
|
-// try {
|
|
|
-// Archive.setCharset(archive, StandardCharsets.UTF_8.name().toByteArray())
|
|
|
-// Archive.readSupportFilterAll(archive)
|
|
|
-// Archive.readSupportFormatAll(archive)
|
|
|
-// Archive.readSetCallbackData(archive, null)
|
|
|
-// val buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE)
|
|
|
-// Archive.readSetReadCallback<Any?>(archive) { _, _ ->
|
|
|
-// buffer.clear()
|
|
|
-// val bytesRead = try {
|
|
|
-// inputStream.read(buffer.array())
|
|
|
-// } catch (e: IOException) {
|
|
|
-// throw ArchiveException(Archive.ERRNO_FATAL, "InputStream.read", e)
|
|
|
-// }
|
|
|
-// if (bytesRead != -1) {
|
|
|
-// buffer.limit(bytesRead)
|
|
|
-// buffer
|
|
|
-// } else {
|
|
|
-// null
|
|
|
-// }
|
|
|
-// }
|
|
|
-// Archive.readSetSkipCallback<Any?>(archive) { _, _, request ->
|
|
|
-// try {
|
|
|
-// inputStream.skip(request)
|
|
|
-// } catch (e: IOException) {
|
|
|
-// throw ArchiveException(Archive.ERRNO_FATAL, "InputStream.skip", e)
|
|
|
-// }
|
|
|
-// }
|
|
|
-// Archive.readOpen1(archive)
|
|
|
-// successful = true
|
|
|
-// return archive
|
|
|
-//
|
|
|
-//
|
|
|
-// } finally {
|
|
|
-// if (!successful) {
|
|
|
-// Archive.free(archive)
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// }
|
|
|
+ @Throws(ArchiveException::class)
|
|
|
+ fun openArchive(
|
|
|
+ inputStream: InputStream,
|
|
|
+ ): Long {
|
|
|
+ val archive: Long = Archive.readNew()
|
|
|
+ var successful = false
|
|
|
+ try {
|
|
|
+ Archive.setCharset(archive, StandardCharsets.UTF_8.name().toByteArray())
|
|
|
+ Archive.readSupportFilterAll(archive)
|
|
|
+ Archive.readSupportFormatAll(archive)
|
|
|
+ Archive.readSetCallbackData(archive, null)
|
|
|
+ val buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE)
|
|
|
+ Archive.readSetReadCallback<Any?>(archive) { _, _ ->
|
|
|
+ buffer.clear()
|
|
|
+ val bytesRead = try {
|
|
|
+ inputStream.read(buffer.array())
|
|
|
+ } catch (e: IOException) {
|
|
|
+ throw ArchiveException(Archive.ERRNO_FATAL, "InputStream.read", e)
|
|
|
+ }
|
|
|
+ if (bytesRead != -1) {
|
|
|
+ buffer.limit(bytesRead)
|
|
|
+ buffer
|
|
|
+ } else {
|
|
|
+ null
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Archive.readSetSkipCallback<Any?>(archive) { _, _, request ->
|
|
|
+ try {
|
|
|
+ inputStream.skip(request)
|
|
|
+ } catch (e: IOException) {
|
|
|
+ throw ArchiveException(Archive.ERRNO_FATAL, "InputStream.skip", e)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Archive.readOpen1(archive)
|
|
|
+ successful = true
|
|
|
+ return archive
|
|
|
+
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ if (!successful) {
|
|
|
+ Archive.free(archive)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
@Throws(ArchiveException::class)
|
|
|
@@ -208,7 +209,9 @@ object LibArchiveUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 解压文件
|
|
|
+ */
|
|
|
@Throws(NullPointerException::class, SecurityException::class)
|
|
|
fun unArchive(
|
|
|
pfd: ParcelFileDescriptor,
|
|
|
@@ -218,14 +221,16 @@ object LibArchiveUtils {
|
|
|
return unArchive(openArchive(pfd), destDir, filter)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 解压
|
|
|
+ */
|
|
|
@Throws(NullPointerException::class, SecurityException::class)
|
|
|
private fun unArchive(
|
|
|
archive: Long,
|
|
|
destDir: File?,
|
|
|
filter: ((String) -> Boolean)? = null
|
|
|
): List<File> {
|
|
|
- destDir ?: throw NullPointerException("解决路径不能为空")
|
|
|
+ destDir ?: throw NullPointerException("解压路径不能为空")
|
|
|
val files = arrayListOf<File>()
|
|
|
|
|
|
|
|
|
@@ -275,13 +280,8 @@ object LibArchiveUtils {
|
|
|
Archive.free(archive)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
return files
|
|
|
|
|
|
-
|
|
|
}
|
|
|
|
|
|
fun getFilesName(pfd: ParcelFileDescriptor, filter: ((String) -> Boolean)?): List<String> {
|