gzipMemory.frink

Download or view gzipMemory.frink in plain text format


/** This contains sample routines to gzip compress and gunzip decompress in
    memory, that is, without writing a file.   It leverages Java's
    ByteArrayInputStream and ByteArrayOutput stream.  */


/** Convert a string into a Java array of bytes.   The inverse function is
    gunzipBytesToString. */

gzipStringToBytes[str is isString, encoding="UTF-8"] :=
{
   baos = newJava["java.io.ByteArrayOutputStream"]
   gz = gzip[baos]
   w = new Writer[gz, encoding]
   w.print[str]
   w.close[]
   return baos.toByteArray[]
}

/** Uncompresses a gzipped Java array of bytes into a string, such as that
    provided by the gzipStringToBytes function. */

gunzipBytesToString[bytes, encoding="UTF-8"] :=
{
   bais = newJava["java.io.ByteArrayInputStream", [bytes]]
   ugz = gunzip[bais]
   return read[ugz, encoding]
}


Download or view gzipMemory.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19975 days, 4 hours, 33 minutes ago.