app: track raw stream in openProgressStream

Wrap the underlying raw source stream in ProgressInputStream before
decompression and compare against rawSize (entry.compressedSize for
DEFLATED zip entries) so that progress percentages and transfer speeds
reflect actual I/O instead of skewed decompressed output on sparse
images.

Also remove the redundant InflaterInputStream.close() override in
ZipChannel since Android's libcore unconditionally ends the Inflater
on close().

Assisted-by: Gemini 4 Argon
This commit is contained in:
topjohnwu
2026-10-14 23:44:30 +02:00
parent 2168e71f14
commit 248195c975
3 changed files with 39 additions and 37 deletions
@@ -215,33 +215,35 @@ abstract class MagiskInstallImpl protected constructor(
}
private fun PatchFileClassifier.openProgressStream(): InputStream {
val max = size
val max = rawSize
val total = max.toFloat() / 1048576
var prevBytes = 0L
var prevTime = 0L
var smoothedSpeed = -1f
return ProgressInputStream(openStream()) { bytes ->
val now = System.currentTimeMillis()
val dt = now - prevTime
if (prevTime == 0L || dt <= 0) {
return openStream { raw ->
ProgressInputStream(raw) { bytes ->
val now = System.currentTimeMillis()
val dt = now - prevTime
if (prevTime == 0L || dt <= 0) {
prevBytes = bytes
prevTime = now
return@ProgressInputStream
}
val instantSpeed = (bytes - prevBytes).toFloat() * 1000 / dt / 1048576
smoothedSpeed = if (smoothedSpeed < 0f) {
instantSpeed
} else {
0.3f * instantSpeed + 0.7f * smoothedSpeed
}
prevBytes = bytes
prevTime = now
return@ProgressInputStream
}
val instantSpeed = (bytes - prevBytes).toFloat() * 1000 / dt / 1048576
smoothedSpeed = if (smoothedSpeed < 0f) {
instantSpeed
} else {
0.3f * instantSpeed + 0.7f * smoothedSpeed
}
prevBytes = bytes
prevTime = now
val progress = bytes.toFloat() / 1048576
if (max > 0) {
val pct = bytes * 100 / max
console.add("\r%.2f / %.2f MB (%d%%) - %.2f MB/s".format(progress, total, pct, smoothedSpeed))
} else {
console.add("\r%.2f MB / ?? - %.2f MB/s".format(progress, smoothedSpeed))
val progress = bytes.toFloat() / 1048576
if (max > 0) {
val pct = bytes * 100 / max
console.add("\r%.2f / %.2f MB (%d%%) - %.2f MB/s".format(progress, total, pct, smoothedSpeed))
} else {
console.add("\r%.2f MB / ?? - %.2f MB/s".format(progress, smoothedSpeed))
}
}
}
}
@@ -3,6 +3,7 @@ package com.topjohnwu.magisk.core.tasks
import com.topjohnwu.magisk.core.utils.DataChannel
import com.topjohnwu.magisk.core.utils.ZipChannel
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipMethod
import java.io.Closeable
import java.io.IOException
import java.io.InputStream
@@ -62,6 +63,11 @@ class PatchFileClassifier @Throws(IOException::class) constructor(
/** Decompressed size of the payload. */
val size: Long get() = entry?.size ?: channel.size()
/** Raw (compressed) size of the payload in the source channel. */
val rawSize: Long get() = entry?.let {
if (it.method == ZipMethod.DEFLATED.code) it.compressedSize else it.size
} ?: channel.size()
/**
* Path of the payload within (nested) zip archives, null if the input itself is the payload.
*/
@@ -82,9 +88,9 @@ class PatchFileClassifier @Throws(IOException::class) constructor(
* The caller is responsible for closing the returned stream.
*/
@Throws(IOException::class)
fun openStream(): InputStream {
val entry = entry ?: return channel.stream()
return zips.last().openStream(entry)
fun openStream(rawWrapper: (InputStream) -> InputStream = { it }): InputStream {
val entry = entry ?: return rawWrapper(channel.stream())
return zips.last().openStream(entry, rawWrapper)
}
override fun close() {
@@ -56,21 +56,15 @@ class ZipChannel(private val source: DataChannel) : Closeable {
* The caller is responsible for closing the returned stream.
*/
@Throws(IOException::class)
fun openStream(entry: ZipArchiveEntry): InputStream {
fun openStream(
entry: ZipArchiveEntry,
rawWrapper: (InputStream) -> InputStream = { it }
): InputStream {
return when (entry.method) {
ZipMethod.STORED.code -> source.stream(dataOffset(entry), entry.size)
ZipMethod.STORED.code -> rawWrapper(source.stream(dataOffset(entry), entry.size))
ZipMethod.DEFLATED.code -> {
val inflater = Inflater(true)
val raw = source.stream(dataOffset(entry), entry.compressedSize)
object : InflaterInputStream(raw, inflater, BUFFER_SIZE) {
override fun close() {
try {
super.close()
} finally {
inflater.end()
}
}
}
val raw = rawWrapper(source.stream(dataOffset(entry), entry.compressedSize))
InflaterInputStream(raw, Inflater(true), BUFFER_SIZE)
}
else -> throw IOException("Unsupported compression method ${entry.method}: ${entry.name}")
}