Stream and process module zips

This commit is contained in:
topjohnwu
2019-07-20 21:04:06 -07:00
parent 746a1d8d59
commit 8ca188f4d4
13 changed files with 99 additions and 270 deletions

View File

@@ -95,10 +95,12 @@ fun File.provide(context: Context = get()): Uri {
}
fun File.mv(destination: File) {
inputStream().copyTo(destination)
inputStream().writeTo(destination)
deleteRecursively()
}
fun String.toFile() = File(this)
fun Intent.chooser(title: String = "Pick an app") = Intent.createChooser(this, title)
fun Intent.chooser(title: String = "Pick an app") = Intent.createChooser(this, title)
fun Context.cachedFile(name: String) = File(cacheDir, name)

View File

@@ -2,8 +2,6 @@ package com.topjohnwu.magisk.utils
import android.net.Uri
import androidx.core.net.toFile
import org.kamranzafar.jtar.TarInputStream
import org.kamranzafar.jtar.TarOutputStream
import java.io.File
import java.io.InputStream
import java.io.OutputStream
@@ -18,12 +16,10 @@ fun ZipInputStream.forEach(callback: (ZipEntry) -> Unit) {
}
}
fun Uri.copyTo(file: File) = toFile().copyTo(file)
fun InputStream.copyTo(file: File) =
withStreams(this, file.outputStream()) { reader, writer -> reader.copyTo(writer) }
fun Uri.writeTo(file: File) = toFile().copyTo(file)
fun File.tarInputStream() = TarInputStream(inputStream())
fun File.tarOutputStream() = TarOutputStream(this)
fun InputStream.writeTo(file: File) =
withStreams(this, file.outputStream()) { reader, writer -> reader.copyTo(writer) }
inline fun <In : InputStream, Out : OutputStream> withStreams(
inStream: In,

View File

@@ -1,8 +1,10 @@
package com.topjohnwu.magisk.utils
import android.content.Context
import com.topjohnwu.superuser.internal.UiThreadHandler
import okhttp3.ResponseBody
import java.io.File
import java.io.FilterInputStream
import java.io.InputStream
import java.io.OutputStream
@@ -32,8 +34,6 @@ inline fun InputStream.writeTo(output: OutputStream, progress: (Long) -> Unit =
fun ResponseBody.writeToString() = string()
fun Context.cachedFile(name: String) = File(cacheDir, name)
inline fun InputStream.copyToWithProgress(
out: OutputStream,
progressEmitter: (Long) -> Unit,
@@ -49,4 +49,32 @@ inline fun InputStream.copyToWithProgress(
progressEmitter(bytesCopied)
}
return bytesCopied
}
class ProgInputStream(base: InputStream,
val progressEmitter: (Long) -> Unit = {}) : FilterInputStream(base) {
private var bytesRead : Long = 0
override fun read(): Int {
val b = read()
if (b >= 0) {
bytesRead++
UiThreadHandler.run { progressEmitter(bytesRead) }
}
return b
}
override fun read(b: ByteArray): Int {
return read(b, 0, b.size)
}
override fun read(b: ByteArray, off: Int, len: Int): Int {
val sz = super.read(b, off, len)
if (sz > 0) {
bytesRead += sz
UiThreadHandler.run { progressEmitter(bytesRead) }
}
return sz
}
}