From 248195c97505a7c5e3b4b9a3ee386e3893d21215 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 14 Oct 2026 19:47:12 +0200 Subject: [PATCH] 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 --- .../magisk/core/tasks/MagiskInstaller.kt | 44 ++++++++++--------- .../magisk/core/tasks/PatchFileClassifier.kt | 12 +++-- .../topjohnwu/magisk/core/utils/ZipChannel.kt | 20 +++------ 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index 0f45f1ca7..a55e8a107 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -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)) + } } } } diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/PatchFileClassifier.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/PatchFileClassifier.kt index 3c394c3ba..2688e1b0c 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/PatchFileClassifier.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/PatchFileClassifier.kt @@ -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() { diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ZipChannel.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ZipChannel.kt index a55946847..ddef46189 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ZipChannel.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ZipChannel.kt @@ -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}") }