Created
July 8, 2011 10:20
-
-
Save bdarfler/1071530 to your computer and use it in GitHub Desktop.
GZIP Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
And
scala> val bytes = ungzip(gzip("hi".getBytes()) ++ gzip("bye".getBytes()))
bytes: Array[Byte] = Array(104, 105)