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

Extract assets from apk file #602

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ import suwayomi.tachidesk.manga.model.table.SourceTable
import suwayomi.tachidesk.server.ApplicationDirs
import uy.kohesive.injekt.injectLazy
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream

object Extension {
private val logger = KotlinLogging.logger {}
Expand Down Expand Up @@ -136,6 +140,7 @@ object Extension {
logger.debug("Main class for extension is $className")

dex2jar(apkFilePath, jarFilePath, fileNameWithoutType)
extractAssetsFromApk(apkFilePath, jarFilePath)

// clean up
File(apkFilePath).delete()
Expand Down Expand Up @@ -198,6 +203,55 @@ object Extension {
}
}

private fun extractAssetsFromApk(apkPath: String, jarPath: String) {
val apkFile = File(apkPath)
val jarFile = File(jarPath)

val assetsFolder = File("${apkFile.parent}/${apkFile.nameWithoutExtension}_assets")
assetsFolder.mkdir()
ZipInputStream(apkFile.inputStream()).use { zipInputStream ->
var zipEntry = zipInputStream.nextEntry
while (zipEntry != null) {
if (zipEntry.name.startsWith("assets/")) {
val assetFile = File(assetsFolder, zipEntry.name.substringAfter("assets/"))
assetFile.parentFile.mkdirs()
FileOutputStream(assetFile).use { outputStream ->
zipInputStream.copyTo(outputStream)
}
}
zipEntry = zipInputStream.nextEntry
}
}

val tempJarFile = File("${jarFile.parent}/${jarFile.nameWithoutExtension}_temp.jar")
ZipInputStream(jarFile.inputStream()).use { jarZipInputStream ->
ZipOutputStream(FileOutputStream(tempJarFile)).use { jarZipOutputStream ->
var zipEntry = jarZipInputStream.nextEntry
while (zipEntry != null) {
if (!zipEntry.name.startsWith("META-INF/")) {
jarZipOutputStream.putNextEntry(ZipEntry(zipEntry.name))
jarZipInputStream.copyTo(jarZipOutputStream)
}
zipEntry = jarZipInputStream.nextEntry
}
assetsFolder.walkTopDown().forEach { file ->
if (file.isFile) {
jarZipOutputStream.putNextEntry(ZipEntry("assets/${file.relativeTo(assetsFolder)}"))
file.inputStream().use { inputStream ->
inputStream.copyTo(jarZipOutputStream)
}
jarZipOutputStream.closeEntry()
}
}
}
}

jarFile.delete()
tempJarFile.renameTo(jarFile)

assetsFolder.deleteRecursively()
}

private val network: NetworkHelper by injectLazy()

private suspend fun downloadAPKFile(url: String, savePath: String) {
Expand Down