Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use apache compression for downloaded module and avoid decompression #8380

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ dependencies {
api(libs.timber)
api(libs.markwon.core)
implementation(libs.bcpkix)
implementation(libs.commons.compress)
implementation(libs.commons.io)
implementation(libs.commons.codec)
implementation(libs.commons.lang)
implementation(files("libs/commons-compress-1.27.2-SNAPSHOT.jar"))

api(libs.libsu.core)
api(libs.libsu.service)
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.topjohnwu.magisk.core.isRunningAsStub
import com.topjohnwu.magisk.core.ktx.cachedFile
import com.topjohnwu.magisk.core.ktx.copyAll
import com.topjohnwu.magisk.core.ktx.copyAndClose
import com.topjohnwu.magisk.core.ktx.forEach
import com.topjohnwu.magisk.core.ktx.set
import com.topjohnwu.magisk.core.ktx.withStreams
import com.topjohnwu.magisk.core.ktx.writeTo
Expand All @@ -43,14 +42,15 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import okhttp3.ResponseBody
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
import timber.log.Timber
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream

/**
* This class drives the execution of file downloads and notification management.
Expand Down Expand Up @@ -308,26 +308,31 @@ class DownloadEngine(
}

private suspend fun handleModule(src: InputStream, file: Uri) {
val input = ZipInputStream(src)
val output = ZipOutputStream(file.outputStream())
val input = ZipArchiveInputStream(src)
val output = ZipArchiveOutputStream(file.outputStream())

withStreams(input, output) { zin, zout ->
zout.putNextEntry(ZipEntry("META-INF/"))
zout.putNextEntry(ZipEntry("META-INF/com/"))
zout.putNextEntry(ZipEntry("META-INF/com/google/"))
zout.putNextEntry(ZipEntry("META-INF/com/google/android/"))
zout.putNextEntry(ZipEntry("META-INF/com/google/android/update-binary"))
zout.putArchiveEntry(ZipArchiveEntry("META-INF/"))
zout.putArchiveEntry(ZipArchiveEntry("META-INF/com/"))
zout.putArchiveEntry(ZipArchiveEntry("META-INF/com/google/"))
zout.putArchiveEntry(ZipArchiveEntry("META-INF/com/google/android/"))
zout.putArchiveEntry(ZipArchiveEntry("META-INF/com/google/android/update-binary"))
context.assets.open("module_installer.sh").use { it.copyAll(zout) }

zout.putNextEntry(ZipEntry("META-INF/com/google/android/updater-script"))
zout.putArchiveEntry(ZipArchiveEntry("META-INF/com/google/android/updater-script"))
zout.write("#MAGISK\n".toByteArray())

zin.forEach { entry ->
for (entry in zin) {
val path = entry.name
if (path.isNotEmpty() && !path.startsWith("META-INF")) {
zout.putNextEntry(ZipEntry(path))
if (!entry.isDirectory) {
zin.copyAll(zout)
// Force STORED method to avoid decompression for better performance
val archiveEntry = ZipArchiveEntry(entry)
entry.method = ZipEntry.STORED
entry.size = entry.compressedSize
zout.addRawArchiveEntry(archiveEntry, zin)
} else {
zout.putArchiveEntry(entry)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ProgressInputStream(
}

override fun read(): Int {
val b = read()
val b = super.read()
if (b >= 0) {
bytesRead++
emitProgress()
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ room = "2.6.1"

[libraries]
bcpkix = { module = "org.bouncycastle:bcpkix-jdk18on", version = "1.78.1" }
commons-compress = { module = "org.apache.commons:commons-compress", version = "1.27.1" }
commons-io = { module = "commons-io:commons-io", version = "2.16.1" }
commons-codec = { module = "commons-codec:commons-codec", version = "1.17.1" }
commons-lang = { module = "org.apache.commons:commons-lang3", version = "3.17.0" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit-moshi = { module = "com.squareup.retrofit2:converter-moshi", version.ref = "retrofit" }
retrofit-scalars = { module = "com.squareup.retrofit2:converter-scalars", version.ref = "retrofit" }
Expand Down