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
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}
import java.util.zip.{GZIPInputStream, GZIPOutputStream}
import scala.util.Try
/**
* aped from https://gist.github.com/owainlewis/1e7d1e68a6818ee4d50e (gzip compression)
* and from https://stackoverflow.com/a/39371571/918858 (ser/deser from Array[Byte])
*/
object Gzip {
/**
* serialize [[T]] to byte array
*
* @param value
* @tparam T
* @return
*/
def serialize[T](value: T): Array[Byte] = {
val stream: ByteArrayOutputStream = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(stream)
oos.writeObject(value)
oos.close()
stream.toByteArray
}
/**
* deserialie from byte array to [[T]]
*
* @param bytes
* @tparam T
* @return
*/
def deserialize[T](bytes: Array[Byte]): T = {
val ois = new ObjectInputStream(new ByteArrayInputStream(bytes))
val value = ois.readObject.asInstanceOf[T]
ois.close()
value
}
/**
* compress from [[T]]
*
* @param input
* @tparam T
* @return
*/
def compress[T](input: T): Array[Byte] = ByteArray.compress(serialize(input))
/**
* decompress to [[T]]
*
* @param input
* @tparam T
* @return
*/
def decompress[T](input: Array[Byte]): Option[T] = ByteArray.decompress(input).map(deserialize[T])
object ByteArray {
/**
* generic byte array compression
*
* @param input
* @return
*/
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
}
/**
* generic byte array decompression
*
* @param compressed
* @return
*/
def decompress(compressed: Array[Byte]): Option[Array[Byte]] =
Try {
val inputStream = new GZIPInputStream(new ByteArrayInputStream(compressed))
org.apache.commons.io.IOUtils.toByteArray(inputStream)
}.toOption
}
}
class GzipSpec extends WordSpecLike with Matchers {
"The GZIP object" should {
"decompress a compressed string" in {
val input = Gzip.compress("Hello World".getBytes("UTF-8"))
Gzip.decompress(input) shouldBe Some("Hello World")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment