I'm trying to download and decompress a gzip file via Ktor + Okio, but apparently that's only available for Android, so I can't import GzipSource (for example) in the shared KMM module. Is there any other way to do that without having to use expect/actual to provide separate Android and iOS implementations?
Guilherme Krzisch
05/24/2023, 6:53 PM
This is how I did on Android, but that requires
<http://java.io|java.io>.*
classes:
Copy code
private val client = HttpClient {
install(HttpTimeout) {
requestTimeoutMillis = 30_000
connectTimeoutMillis = 30_000
socketTimeoutMillis = 30_000
}
}
@Suppress("BlockingMethodInNonBlockingContext")
actual suspend fun downloadAndDecompressGzipFile(url: String, destination: Path) {
val response = client.get(url)
val inputStream = BufferedInputStream(InputSource(GZIPInputStream(response.bodyAsChannel().toInputStream())).byteStream)
val outputStream = BufferedOutputStream(FileOutputStream(destination.toFile()))
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
inputStream.close()
}