Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

mathiasbynens commented Oct 9, 2013

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

@davidmurdoch

This comment has been minimized.

Copy link
Owner Author

davidmurdoch commented Oct 9, 2013

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
You can’t perform that action at this time.