Skip to content

Instantly share code, notes, and snippets.

@fairjm
Created July 20, 2015 08:01
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 fairjm/6ae5e3d742dc3a0ef9ce to your computer and use it in GitHub Desktop.
Save fairjm/6ae5e3d742dc3a0ef9ce to your computer and use it in GitHub Desktop.
open System.Text
open System.IO
open System.IO.Compression
let iso8859_1 = Encoding.GetEncoding("ISO-8859-1");
let compress (s:string) =
let sBytes = Encoding.UTF8.GetBytes(s)
use original = new MemoryStream(sBytes)
let oBytes = using(new MemoryStream())(fun out ->
using(new GZipStream(out,CompressionMode.Compress))(fun gzip ->
original.CopyTo(gzip)
)
out.ToArray()
)
iso8859_1.GetString(oBytes)
let decompress (s:string) =
let sBytes = iso8859_1.GetBytes(s)
use compressed = new MemoryStream(sBytes)
let deBytes = using(new MemoryStream())(fun de ->
using(new GZipStream(compressed,CompressionMode.Decompress))(fun gzip ->
gzip.CopyTo(de)
)
de.ToArray()
)
Encoding.UTF8.GetString(deBytes)
[<EntryPoint>]
let main argv =
compress "just for test by fairjm" |> decompress |>printfn "%s"
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment