GZip byte array using System.IO.Compression in .NET Here is how to compress/uncompress a byte array using .NET's GZip ability: public static class CompressionHelper { /// <summary> /// Compress the byte[] /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] Compress(byte[] input) { byte[] output; using (MemoryStream ms = new MemoryStream()) { using(GZipStream gs = new GZipStream(ms, CompressionMode.Compress)) { gs.Write(input, 0, input.Length); gs.Close(); output = ms.ToArray(); } ms.Close(); } return output; } /// <summary> /// Decompress the byte[] /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] Decompress(byte[] input) { List<byte> output = new List <byte> (); using (MemoryStream ms = new MemoryStream(input)) { using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress)) { int readByte = gs.ReadByte(); while(readByte != -1) { output.Add((byte)readByte); readByte = gs.ReadByte(); } gs.Close(); } ms.Close(); } return output.ToArray(); } } // class
Created By: amos 7/30/2009 6:40:28 PM Updated: 4/2/2013 2:55:58 PM
|
|