Skip to content

Instantly share code, notes, and snippets.

@dalegaspi
Forked from owainlewis/Gzip.scala
Last active May 4, 2018 13:13
Show Gist options
  • Save dalegaspi/9f0c98ee68f1475ff6cf8b5ac9f512e6 to your computer and use it in GitHub Desktop.
Save dalegaspi/9f0c98ee68f1475ff6cf8b5ac9f512e6 to your computer and use it in GitHub Desktop.
Gzip Scala
import java.io.{ByteArrayOutputStream, ByteArrayInputStream}
import java.util.zip.{GZIPOutputStream, GZIPInputStream}
import scala.util.Try
object Gzip {
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