Skip to content

Instantly share code, notes, and snippets.

@stonegao
Forked from bdarfler/gzip_test.scala
Created December 1, 2011 04:10
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 stonegao/1413513 to your computer and use it in GitHub Desktop.
Save stonegao/1413513 to your computer and use it in GitHub Desktop.
GZIP Test
import java.io.ByteArrayInputStream
import java.util.zip.GZIPOutputStream
import java.util.zip.GZIPInputStream
import java.io.ByteArrayOutputStream
val buffer = new Array[Byte](1024*4)
def gzip(bytes: Array[Byte]) = {
val byteIn = new ByteArrayInputStream(bytes)
val byteOut = new ByteArrayOutputStream()
val gzipOut = new GZIPOutputStream(byteOut)
var n = byteIn.read(buffer)
while (-1 != n) {
gzipOut.write(buffer, 0, n);
n = byteIn.read(buffer)
}
gzipOut.close()
byteOut.toByteArray()
}
def ungzip(bytes: Array[Byte]) = {
val byteIn = new ByteArrayInputStream(bytes)
val gzipIn = new GZIPInputStream(byteIn)
val byteOut = new ByteArrayOutputStream()
var n = gzipIn.read(buffer)
while (-1 != n) {
byteOut.write(buffer, 0, n);
n = gzipIn.read(buffer)
}
byteOut.toByteArray()
}
val bytes = ungzip(gzip("hi".getBytes()) ++ gzip("bye".getBytes()))
println(new String(bytes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment