Skip to content

Instantly share code, notes, and snippets.

@davidmurdoch
Created October 9, 2013 12:25
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 davidmurdoch/6900441 to your computer and use it in GitHub Desktop.
Save davidmurdoch/6900441 to your computer and use it in GitHub Desktop.
The code behind my snippet deflator (http://www.vervestudios.co/projects/compression-tests/snippet-deflator). Original written sometime in 2009.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form != null && Request.Form.Count > 0 && Request.Form["text"] != null)
{
var text = Request.Form["text"];
byte[] byteArray = Encoding.ASCII.GetBytes(text);
using (var stream = new MemoryStream())
{
using (var strm = new zlib.ZOutputStream(stream, zlib.zlibConst.Z_BEST_COMPRESSION, false))
{
strm.Write(byteArray, 0, byteArray.Length);
strm.Flush();
strm.finish();
strm.Position = 0;
var length = stream.Length;
var olength = byteArray.Length;
decimal perc = 100 - Math.Round((length / (decimal)olength) * 100, 2);
size = "<br />Deflated Length is: " + length + " bytes.<br /> Original length is: " + olength + " bytes.<br />" + perc + "% savings";
}
}
}
}
@mathiasbynens
Copy link

Z_BEST_COMPRESSION — I see the zlib module was written by a Frenchman.

@davidmurdoch
Copy link
Author

Haha. Sorry about the C#.

Apache doesn't have a built in way to get a raw deflate stream, at least not at the time I wrote this.
At the time I didn't check for any deflate libs written in other languages, but I imagine they'll supply similar results.

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