Last active
January 7, 2025 15:26
-
-
Save sgdan/eaada2f243a48196c5d4e49a277e3880 to your computer and use it in GitHub Desktop.
Kotlin code to compress/uncompress a string with gzip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream | |
import java.io.File | |
import java.nio.charset.StandardCharsets.UTF_8 | |
import java.util.zip.GZIPInputStream | |
import java.util.zip.GZIPOutputStream | |
fun gzip(content: String): ByteArray { | |
val bos = ByteArrayOutputStream() | |
GZIPOutputStream(bos).bufferedWriter(UTF_8).use { it.write(content) } | |
return bos.toByteArray() | |
} | |
fun ungzip(content: ByteArray): String = | |
GZIPInputStream(content.inputStream()).bufferedReader(UTF_8).use { it.readText() } | |
//val content = "a string with some content" | |
val file = File("gzip.kts") | |
val content = file.readText() | |
println("size of original: ${content.length}") | |
val zipped = gzip(content) | |
println("size zipped: ${zipped.size}") | |
val unzipped = ungzip(zipped) | |
println("size unzipped: ${unzipped.length}") | |
assert(unzipped == content) |
π π
Thank you !
Thank you so much!
you saved me!
amen
π
π
π
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π