Skip to content

Instantly share code, notes, and snippets.

@dalegaspi
Forked from owainlewis/Gzip.scala
Last active May 4, 2018 13:13

Revisions

  1. dalegaspi revised this gist May 4, 2018. 1 changed file with 80 additions and 16 deletions.
    96 changes: 80 additions & 16 deletions Gzip.scala
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,87 @@
    import java.io.{ByteArrayOutputStream, ByteArrayInputStream}
    import java.util.zip.{GZIPOutputStream, GZIPInputStream}

    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 {

    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
    /**
    * 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
    }

    def decompress(compressed: Array[Byte]): Option[String] =
    Try {
    val inputStream = new GZIPInputStream(new ByteArrayInputStream(compressed))
    scala.io.Source.fromInputStream(inputStream).mkString
    }.toOption
    /**
    * 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
    }
    }
  2. @owainlewis owainlewis revised this gist Mar 16, 2016. 2 changed files with 10 additions and 0 deletions.
    File renamed without changes.
    10 changes: 10 additions & 0 deletions GzipSpec.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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")
    }
    }
    }
  3. @owainlewis owainlewis created this gist Mar 16, 2016.
    23 changes: 23 additions & 0 deletions GZIP.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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
    }