Skip to content

Instantly share code, notes, and snippets.

@sgdan sgdan/gzip.kts
Last active Jul 9, 2020

What would you like to do?
Kotlin code to compress/uncompress a string with gzip
import java.nio.charset.StandardCharsets.UTF_8
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)

This comment has been minimized.

Copy link

kirillgroshkov commented Nov 25, 2019



This comment has been minimized.

Copy link

dvalvezon commented Mar 11, 2020

👍 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.