mirror of
https://github.com/ReVanced/revanced-cli.git
synced 2026-01-27 21:21:06 +00:00
fix: resource patcher
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
package app.revanced.utils.filesystem
|
||||
|
||||
import java.io.Closeable
|
||||
import java.io.File
|
||||
import java.nio.file.FileSystem
|
||||
import java.nio.file.FileSystems
|
||||
import java.nio.file.Files
|
||||
|
||||
internal class FileSystemUtils(
|
||||
file: File
|
||||
) : Closeable {
|
||||
private var fileSystem: FileSystem
|
||||
|
||||
init {
|
||||
fileSystem = FileSystems.newFileSystem(file.toPath(), null as ClassLoader?)
|
||||
}
|
||||
|
||||
private fun deleteDirectory(dirPath: String) {
|
||||
val files = Files.walk(fileSystem.getPath("$dirPath/"))
|
||||
|
||||
files
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.forEach {
|
||||
|
||||
Files.delete(it)
|
||||
}
|
||||
|
||||
files.close()
|
||||
}
|
||||
|
||||
|
||||
internal fun replaceDirectory(replacement: File) {
|
||||
if (!replacement.isDirectory) throw Exception("${replacement.name} is not a directory.")
|
||||
|
||||
// FIXME: make this delete the directory recursively
|
||||
//deleteDirectory(replacement.name)
|
||||
//val path = Files.createDirectory(fileSystem.getPath(replacement.name))
|
||||
|
||||
val excludeFromPath = replacement.path.removeSuffix(replacement.name)
|
||||
for (path in Files.walk(replacement.toPath())) {
|
||||
val file = path.toFile()
|
||||
if (file.isDirectory) {
|
||||
val relativePath = path.toString().removePrefix(excludeFromPath)
|
||||
val fileSystemPath = fileSystem.getPath(relativePath)
|
||||
if (!Files.exists(fileSystemPath)) Files.createDirectory(fileSystemPath)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
replaceFile(path.toString().removePrefix(excludeFromPath), file.readBytes())
|
||||
}
|
||||
}
|
||||
|
||||
internal fun replaceFile(sourceFile: String, content: ByteArray) {
|
||||
val path = fileSystem.getPath(sourceFile)
|
||||
Files.deleteIfExists(path)
|
||||
Files.write(path, content)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
fileSystem.close()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package app.revanced.utils.filesystem
|
||||
|
||||
import java.io.Closeable
|
||||
import java.io.File
|
||||
import java.nio.file.FileSystems
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.util.zip.ZipEntry
|
||||
|
||||
internal class ZipFileSystemUtils(
|
||||
file: File
|
||||
) : Closeable {
|
||||
private var zipFileSystem = FileSystems.newFileSystem(file.toPath(), mapOf("noCompression" to true))
|
||||
|
||||
private fun Path.deleteRecursively() {
|
||||
if (Files.isDirectory(this)) {
|
||||
Files.list(this).forEach { path ->
|
||||
path.deleteRecursively()
|
||||
}
|
||||
}
|
||||
|
||||
Files.delete(this)
|
||||
}
|
||||
|
||||
internal fun writePathRecursively(path: Path) {
|
||||
Files.list(path).let { fileStream ->
|
||||
fileStream.forEach { filePath ->
|
||||
val fileSystemPath = filePath.getRelativePath(path)
|
||||
fileSystemPath.deleteRecursively()
|
||||
}
|
||||
|
||||
fileStream
|
||||
}.close()
|
||||
|
||||
Files.walk(path).let { fileStream ->
|
||||
fileStream.skip(1).forEach { filePath ->
|
||||
val relativePath = filePath.getRelativePath(path)
|
||||
|
||||
if (Files.isDirectory(filePath)) {
|
||||
Files.createDirectory(relativePath)
|
||||
return@forEach
|
||||
}
|
||||
|
||||
Files.copy(filePath, relativePath)
|
||||
}
|
||||
|
||||
fileStream
|
||||
}.close()
|
||||
}
|
||||
|
||||
internal fun write(path: String, content: ByteArray) = Files.write(zipFileSystem.getPath(path), content)
|
||||
|
||||
private fun Path.getRelativePath(path: Path): Path = zipFileSystem.getPath(path.relativize(this).toString())
|
||||
|
||||
internal fun uncompress(vararg paths: String) =
|
||||
paths.forEach { Files.setAttribute(zipFileSystem.getPath(it), "zip:method", ZipEntry.STORED) }
|
||||
|
||||
override fun close() = zipFileSystem.close()
|
||||
}
|
||||
Reference in New Issue
Block a user