This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
Gleb Koval 1fd3fa8907
All checks were successful
Test Workflow / Lint and test library (pull_request) Successful in 9m10s
Publish Workflow / Publish library (pull_request) Successful in 9m36s
Linting
2023-12-20 15:12:56 +00:00

34 lines
1.1 KiB
Kotlin

package filesystem
import java.nio.file.FileAlreadyExistsException
import java.nio.file.FileSystemException
import java.nio.file.Files
import java.nio.file.Path
class FSCreator {
// Create entry, leaving existing folders' contents, but overwriting existing files.
@Throws(FileSystemException::class)
fun create(
entryToCreate: FSEntry,
destination: String,
) {
val queue = ArrayDeque<Pair<FSEntry, Path>>()
queue.add(entryToCreate to Path.of(destination))
while (queue.isNotEmpty()) {
val (entry, dest) = queue.removeFirst()
val path = dest.resolve(entry.name)
try {
when (entry) {
is FSFile -> Files.createFile(path)
is FSFolder -> Files.createDirectory(path)
}
} catch (_: FileAlreadyExistsException) {
} // Allow files/folders to already exist.
when (entry) {
is FSFile -> Files.write(path, entry.content.toByteArray())
is FSFolder -> queue.addAll(entry.entries.map { it to path })
}
}
}
}