Hey all, does anybody know if a KMP library to do ...
# multiplatform
d
Hey all, does anybody know if a KMP library to do either zip or rar decompression? No need to create, just to extract files from either archive type.
Also a KMP equivalent to Apache Tiki for determining the mime type of a byte stream.
c
m
For which platforms? I still don’t know any library which could do it for all platforms. I am also looking for that feature.
d
For me I’m specifically looking for Android and iOS.
m
For these platforms it is easy. You can do it via Okio like this:
Copy code
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath
import okio.buffer
import okio.openZip
import okio.use

// This code is identical for jvmCommon and iOS.
fun FileSystem.unpackZip(zipFile: Path, destDir: Path) {
    fun Path.createParentDirectories() {
        this.parent?.let { parent ->
            createDirectories(parent)
        }
    }

    val zipFileSystem = openZip(zipFile)
    val paths = zipFileSystem.listRecursively("/".toPath())
        .filter { zipFileSystem.metadata(it).isRegularFile }
        .toList()

    paths.forEach { zipFilePath ->
        zipFileSystem.source(zipFilePath).buffer().use { source ->
            val relativeFilePath = zipFilePath.toString().trimStart('/')
            val fileToWrite = destDir.resolve(relativeFilePath)
            fileToWrite.createParentDirectories()
            sink(fileToWrite).buffer().use { sink ->
                sink.writeAll(source)
            }
        }
    }
}