refactor: ZipFileSystemUtils

This commit is contained in:
oSumAtrIX 2022-06-27 23:48:26 +02:00
parent 8782cdef67
commit 9f91f63220
2 changed files with 30 additions and 38 deletions

View File

@ -25,23 +25,23 @@ internal object Patcher {
args.inputFile.copyTo(output) args.inputFile.copyTo(output)
val result = patcher.save() val result = patcher.save()
val inputFile = if (!args.disableResourcePatching && result.resourceFile != null) { ZipFileSystemUtils(output).use { outputFileSystem ->
result.resourceFile
} else null
ZipFileSystemUtils(inputFile, output).use { fileSystem ->
// replace all dex files // replace all dex files
result.dexFiles.forEach { result.dexFiles.forEach {
logger.info("Writing dex file ${it.name}") logger.info("Writing dex file ${it.name}")
fileSystem.write(it.name, it.dexFileInputStream.readAllBytes()) outputFileSystem.write(it.name, it.dexFileInputStream.readAllBytes())
} }
// inputFile being null implies resource patching being disabled if (!args.disableResourcePatching) {
if (inputFile != null) { logger.info("Writing resources...")
// write resources
logger.info("Writing resources") ZipFileSystemUtils(result.resourceFile!!).use { resourceFileSystem ->
fileSystem.writeInput() val resourceFiles = resourceFileSystem.getFile(File.pathSeparator)
fileSystem.uncompress(*result.doNotCompress!!.toTypedArray()) outputFileSystem.writePathRecursively(resourceFiles)
}
} }
outputFileSystem.uncompress(*result.doNotCompress!!.toTypedArray())
} }
} }
} }

View File

@ -7,15 +7,14 @@ import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
internal class ZipFileSystemUtils(input: File?, output: File) : Closeable { internal class ZipFileSystemUtils(
private val inFileSystem = if (input != null) { file: File
FileSystems.newFileSystem(input.toPath()) ) : Closeable {
} else null private var zipFileSystem = FileSystems.newFileSystem(file.toPath(), mapOf("noCompression" to true))
private val outFileSystem = FileSystems.newFileSystem(output.toPath(), mapOf("noCompression" to true))
private fun Path.deleteRecursively() { private fun Path.deleteRecursively() {
if (!Files.exists(this)) { if (!Files.exists(this)) {
throw IllegalStateException("File exists in input but not in output, cannot delete") throw IllegalStateException("File exists in real folder but not in zip file system")
} }
if (Files.isDirectory(this)) { if (Files.isDirectory(this)) {
@ -27,25 +26,21 @@ internal class ZipFileSystemUtils(input: File?, output: File) : Closeable {
Files.delete(this) Files.delete(this)
} }
internal fun writeInput() { internal fun getFile(path: String) = zipFileSystem.getPath(path)
if (inFileSystem == null) {
throw IllegalArgumentException("Input file not set")
}
val root = inFileSystem.getPath(inFileSystem.separator)
Files.list(root).close() internal fun writePathRecursively(path: Path) {
Files.list(path).use { fileStream ->
Files.list(root).also { fileStream ->
fileStream.forEach { filePath -> fileStream.forEach { filePath ->
val fileSystemPath = filePath.getRelativePath(root) val fileSystemPath = filePath.getRelativePath(path)
fileSystemPath.deleteRecursively() fileSystemPath.deleteRecursively()
} }
}.close() }
Files.walk(root).also { fileStream -> Files.walk(path).use { fileStream ->
// don't include build directory by skipping the root node. // don't include build directory
// by skipping the root node.
fileStream.skip(1).forEach { filePath -> fileStream.skip(1).forEach { filePath ->
val relativePath = filePath.getRelativePath(root) val relativePath = filePath.getRelativePath(path)
if (Files.isDirectory(filePath)) { if (Files.isDirectory(filePath)) {
Files.createDirectory(relativePath) Files.createDirectory(relativePath)
@ -54,18 +49,15 @@ internal class ZipFileSystemUtils(input: File?, output: File) : Closeable {
Files.copy(filePath, relativePath) Files.copy(filePath, relativePath)
} }
}.close() }
} }
internal fun write(path: String, content: ByteArray) = Files.write(outFileSystem.getPath(path), content) internal fun write(path: String, content: ByteArray) = Files.write(zipFileSystem.getPath(path), content)
private fun Path.getRelativePath(path: Path): Path = outFileSystem.getPath(path.relativize(this).toString()) private fun Path.getRelativePath(path: Path): Path = zipFileSystem.getPath(path.relativize(this).toString())
internal fun uncompress(vararg paths: String) = internal fun uncompress(vararg paths: String) =
paths.forEach { Files.setAttribute(outFileSystem.getPath(it), "zip:method", ZipEntry.STORED) } paths.forEach { Files.setAttribute(zipFileSystem.getPath(it), "zip:method", ZipEntry.STORED) }
override fun close() { override fun close() = zipFileSystem.close()
inFileSystem?.close()
outFileSystem.close()
}
} }