Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Created July 8, 2011 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bdarfler/1071530 to your computer and use it in GitHub Desktop.
Save bdarfler/1071530 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))
@etorreborre
Copy link

I get this on Windows:

scala> println(new String(bytes))
hi

@etorreborre
Copy link

And

scala> val bytes = ungzip(gzip("hi".getBytes()) ++ gzip("bye".getBytes()))
bytes: Array[Byte] = Array(104, 105)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment