Skip to content

Commit

Permalink
copy assets to jar directly
Browse files Browse the repository at this point in the history
instead of using a temporary assets folder
  • Loading branch information
beer-psi committed Jan 20, 2024
1 parent 0503f48 commit 9144bf2
Showing 1 changed file with 12 additions and 40 deletions.
52 changes: 12 additions & 40 deletions inspector/src/main/kotlin/inspector/util/Extension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,70 +60,42 @@ object Extension {
dex2jar(apkFile, jarFile)
extractAssetsFromApk(apkFile, jarFile)

val instance = loadExtensionSources(jarFile, className)

// collect sources from the extension
return packageInfo.packageName to when (
val instance =
loadExtensionSources(jarFile, className)
) {
return packageInfo.packageName to when (instance) {
is Source -> listOf(instance).filterIsInstance<HttpSource>()
is SourceFactory -> instance.createSources().filterIsInstance<HttpSource>()
else -> throw RuntimeException("Unknown source class type! ${instance.javaClass}")
}
}

private fun extractAssetsFromApk(apkFile: File, jarFile: File) {
val assetsFolder = File("${apkFile.parent}/${apkFile.nameWithoutExtension}_assets")

assetsFolder.mkdirs()

ZipInputStream(apkFile.inputStream()).use { zis ->
generateSequence { zis.nextEntry }
.filter { it.name.startsWith("assets/") }
.forEach {
val assetFile = File(assetsFolder, it.name)

assetFile.parentFile.mkdirs()
FileOutputStream(assetFile).use { os -> zis.copyTo(os) }
}
zis.closeEntry()
zis.close()
}

val tempJarFile = File("${jarFile.parent}/${jarFile.nameWithoutExtension}_temp.jar")
val zos = ZipOutputStream(FileOutputStream(tempJarFile))

ZipInputStream(jarFile.inputStream()).use { zis ->
val zos = ZipOutputStream(FileOutputStream(tempJarFile))

generateSequence { zis.nextEntry }
.filterNot { it.name.startsWith("META-INF/") }
.forEach {
zos.putNextEntry(ZipEntry(it.name))
zis.copyTo(zos)
zos.closeEntry()
}
zis.close()
}

assetsFolder.walkTopDown()
.filter { it.isFile }
ZipInputStream(apkFile.inputStream()).use { zis ->
generateSequence { zis.nextEntry }
.filter { it.name.startsWith("assets/") }
.forEach {
zos.putNextEntry(ZipEntry(it.relativeTo(assetsFolder).toString().replace('\\', '/')))
it.inputStream().run {
copyTo(zos)
close()
}
zos.closeEntry()
zos.putNextEntry(ZipEntry(it.name))
zis.copyTo(zos)
}

zis.closeEntry()
zis.close()
zos.closeEntry()
zos.close()
}

zos.close()
jarFile.delete()
tempJarFile.renameTo(jarFile)

if (!assetsFolder.deleteRecursively()) {
throw Exception("Could not delete assets folder.")
}
}
}

0 comments on commit 9144bf2

Please sign in to comment.