Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
refactor: ZipFileSystemUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jun 27, 2022
1 parent 8782cde commit 9f91f63
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
22 changes: 11 additions & 11 deletions src/main/kotlin/app/revanced/cli/patcher/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ internal object Patcher {
args.inputFile.copyTo(output)

val result = patcher.save()
val inputFile = if (!args.disableResourcePatching && result.resourceFile != null) {
result.resourceFile
} else null
ZipFileSystemUtils(inputFile, output).use { fileSystem ->
ZipFileSystemUtils(output).use { outputFileSystem ->
// replace all dex files
result.dexFiles.forEach {
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 (inputFile != null) {
// write resources
logger.info("Writing resources")
fileSystem.writeInput()
fileSystem.uncompress(*result.doNotCompress!!.toTypedArray())
if (!args.disableResourcePatching) {
logger.info("Writing resources...")

ZipFileSystemUtils(result.resourceFile!!).use { resourceFileSystem ->
val resourceFiles = resourceFileSystem.getFile(File.pathSeparator)
outputFileSystem.writePathRecursively(resourceFiles)
}
}

outputFileSystem.uncompress(*result.doNotCompress!!.toTypedArray())
}
}
}
46 changes: 19 additions & 27 deletions src/main/kotlin/app/revanced/utils/filesystem/ZipFileSystemUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import java.nio.file.Files
import java.nio.file.Path
import java.util.zip.ZipEntry

internal class ZipFileSystemUtils(input: File?, output: File) : Closeable {
private val inFileSystem = if (input != null) {
FileSystems.newFileSystem(input.toPath())
} else null
private val outFileSystem = FileSystems.newFileSystem(output.toPath(), mapOf("noCompression" to true))
internal class ZipFileSystemUtils(
file: File
) : Closeable {
private var zipFileSystem = FileSystems.newFileSystem(file.toPath(), mapOf("noCompression" to true))

private fun Path.deleteRecursively() {
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)) {
Expand All @@ -27,25 +26,21 @@ internal class ZipFileSystemUtils(input: File?, output: File) : Closeable {
Files.delete(this)
}

internal fun writeInput() {
if (inFileSystem == null) {
throw IllegalArgumentException("Input file not set")
}
val root = inFileSystem.getPath(inFileSystem.separator)

Files.list(root).close()
internal fun getFile(path: String) = zipFileSystem.getPath(path)

Files.list(root).also { fileStream ->
internal fun writePathRecursively(path: Path) {
Files.list(path).use { fileStream ->
fileStream.forEach { filePath ->
val fileSystemPath = filePath.getRelativePath(root)
val fileSystemPath = filePath.getRelativePath(path)
fileSystemPath.deleteRecursively()
}
}.close()
}

Files.walk(root).also { fileStream ->
// don't include build directory by skipping the root node.
Files.walk(path).use { fileStream ->
// don't include build directory
// by skipping the root node.
fileStream.skip(1).forEach { filePath ->
val relativePath = filePath.getRelativePath(root)
val relativePath = filePath.getRelativePath(path)

if (Files.isDirectory(filePath)) {
Files.createDirectory(relativePath)
Expand All @@ -54,18 +49,15 @@ internal class ZipFileSystemUtils(input: File?, output: File) : Closeable {

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) =
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() {
inFileSystem?.close()
outFileSystem.close()
}
override fun close() = zipFileSystem.close()
}

0 comments on commit 9f91f63

Please sign in to comment.