Skip to content

Instantly share code, notes, and snippets.

@FireZenk FireZenk/gzip.kts

forked from sgdan/gzip.kts
Last active Nov 22, 2018
What would you like to do?
Kotlin code to compress/uncompress a string with gzip
import java.nio.charset.StandardCharsets.UTF_8
fun String.gzip(): ByteArray {
val bos = ByteArrayOutputStream()
GZIPOutputStream(bos).bufferedWriter(UTF_8).use { it.write(this) }
return bos.toByteArray()
fun ByteArray.ungzip(): String =
GZIPInputStream(this.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 = content.gzip()
println("size zipped: ${zipped.size}")
val unzipped = zipped.ungzip()
println("size unzipped: ${unzipped.length}")
assert(unzipped == content)
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.