Created
October 9, 2013 12:25
-
-
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.
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
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"; | |
} | |
} | |
} | |
} |
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
Z_BEST_COMPRESSION
— I see the zlib module was written by a Frenchman.