I had to get an object serialized to a byte[] and then compress it using SharpZipLib and gzip and then decompress it and deserialize it to the original object. The serialization was easy but for some reason I was struggling with using the GZipOutputStream and GZipInputStream compression providers in the SharpZipLib library.
Then I found Scott Galloway's compression helper. I highly recommend it. Anyway, here's the code without the helper. Visit Scott's blog for that.
public class ResultSerializer { public static byte[] Serialize(ResultData data) { //convert to byte[] IFormatter frm = new BinaryFormatter(); MemoryStream ms = new MemoryStream(8096); frm.Serialize(ms, data); byte[] serial = ms.ToArray(); ms.Close(); byte[] retval = ZipUtil.Compress(serial); return retval; } public static ResultData Deserialize(byte[] zipData) { byte[] data = ZipUtil.DeCompress(zipData); //now deserialize IFormatter frm = new BinaryFormatter(); MemoryStream datams = new MemoryStream(data, 0, data.Length); ResultData retval = (ResultData)frm.Deserialize(datams); return retval; } }
Note that I renamed Scott's helper "ZipUtil" for my own reasons.
Page rendered at Friday, September 03, 2010 4:07:26 AM (Mountain Daylight Time, UTC-06:00)
DisclaimerThe opinions expressed herein are just that, opinions. Don't have a fit if you think they're wrong. Post your comment or write your own blog.