summaryrefslogtreecommitdiff
path: root/plugin/src/main/kotlin/moe/nea/archenemy/util/FSUtil.kt
blob: 0b2f70e536195741f358f4a1c899a779e19c38b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package moe.nea.archenemy.util

import java.io.InputStream
import java.net.URI
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Path
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
import kotlin.io.path.inputStream
import kotlin.io.path.outputStream

// TODO: Figure out license shit with unimined cause i copied some of these utils?
fun Path.zipFs(): FileSystem {
    if (!exists()) {
        parent.createDirectories()
        ZipOutputStream(this.outputStream()).use { }
    }
    return FileSystems.newFileSystem(URI.create("jar:${toUri()}"), mapOf("create" to true), null)
}

fun Path.readZipFs(reader: (String, InputStream) -> Unit) {
    ZipInputStream(this.inputStream()).use { stream ->
        for (entry in generateSequence { stream.nextEntry }) {
            if (entry.isDirectory) {
                continue
            }
            reader(entry.name, stream)
        }
    }
}