Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
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")
}
}
}
@sbhola

This comment has been minimized.

Copy link

@sbhola sbhola commented Oct 25, 2016

Should you not do Base64Encoding here ?

@mikeokner

This comment has been minimized.

Copy link

@mikeokner mikeokner commented Nov 1, 2016

@sbhola only if you need it in an ASCII string for sending via a transport like SMTP or embedding within some other file format. The raw binary data will be smaller than the Base64 representation.

@stilaye

This comment has been minimized.

Copy link

@stilaye stilaye commented Nov 4, 2016

Is there any dependency we need include to run this code? I am getting following error message"Cannot resolve symbol wordspeclike"?

@JThakrar

This comment has been minimized.

Copy link

@JThakrar JThakrar commented May 12, 2017

Thanks @owainlewis!
This is very nice and useful.
Just wondering - why does decompress return a String and not an Array[Byte]?
I think having Array[Byte] will make this useful for a variety of binary and non binary items.

@phderome

This comment has been minimized.

Copy link

@phderome phderome commented Jul 2, 2017

I have same question as JThakrar.

@JThakrar

This comment has been minimized.

Copy link

@JThakrar JThakrar commented Jan 26, 2018

Here's my version of decompress.

  def decompress(compressed: Array[Byte]): Array[Byte] = {
    val gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressed))
    val output = new ArrayBuffer[Byte]()
    var totalByteCount = 0
    val bytes = new Array[Byte](1024)
    while (gzipInputStream.available() == 1) {
      val byteCount = gzipInputStream.read(bytes)
      if (byteCount > 0) {
        output ++= bytes.take(byteCount)
        totalByteCount += byteCount
      }
    }
    output.take(totalByteCount).toArray
  }
@dalegaspi

This comment has been minimized.

Copy link

@dalegaspi dalegaspi commented May 3, 2018

similar to @JThakrar's...probably simpler but requires Apache Commons IO

def decompress(compressed: Array[Byte]): Option[Array[Byte]] =
    Try {
      val inputStream = new GZIPInputStream(new ByteArrayInputStream(compressed))
      org.apache.commons.io.IOUtils.toByteArray(inputStream)
    }.toOption
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment