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}") }