using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Text; namespace Sample.Common.Util { public static class GZipHelper { public static byte[] Compress(byte[] val) { byte[] result; using (MemoryStream memoryStream = new MemoryStream()) using (GZipStream zipStream = new GZipStream(memoryStream, CompressionMode.Compress)) { zipStream.Write(val, 0, val.Length); zipStream.Close(); result = memoryStream.ToArray(); } return result; } public static byte[] Decompress(byte[] val) { byte[] result = null; using (MemoryStream memoryStream = new MemoryStream(val)) using (GZipStream zipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) { GZipHelper.ReadAllBytesFromStream(zipStream, out result); } return result; } private static void ReadAllBytesFromStream(Stream stream, out byte[] buffer) { using (MemoryStream memoryStream = new MemoryStream()) { int readByte = 0; while (true) { readByte = stream.ReadByte(); if (readByte == -1) { break; } memoryStream.WriteByte((byte)readByte); } buffer = new byte[memoryStream.Length]; buffer = memoryStream.ToArray(); } } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)