public
Created — forked from bdarfler/gzip_test.scala

GZIP Test

  • Download Gist
gzip_test.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.