Skip to content

Instantly share code, notes, and snippets.

@Renkai
Created August 12, 2019 01:44
Show Gist options
  • Save Renkai/b2d260b953ddd8397bab385c13fcb24d to your computer and use it in GitHub Desktop.
Save Renkai/b2d260b953ddd8397bab385c13fcb24d to your computer and use it in GitHub Desktop.
def compress(input: Array[Byte]): Array[Byte] = {
val bos = new ByteArrayOutputStream(input.length)
val gzip = new GZIPOutputStream(bos)
gzip.write(input)
gzip.close()
val compressed = bos.toByteArray
bos.close()
compressed
}
def decompress(compressed: Array[Byte]): Option[String] =
Try {
val inputStream = new GZIPInputStream(new ByteArrayInputStream(compressed))
scala.io.Source.fromInputStream(inputStream).mkString
}.toOption
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment