Skip to content

Instantly share code, notes, and snippets.

@markandey
Created January 4, 2011 10:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save markandey/764648 to your computer and use it in GitHub Desktop.
Save markandey/764648 to your computer and use it in GitHub Desktop.
Simple Compress and Decompress in C#.net
using System.IO;
using System.IO.Compression;
class Qompress
{
public static byte[] CompressBuffer(byte[] byteArray)
{
MemoryStream strm = new MemoryStream();
GZipStream GZipStrem = new GZipStream(strm, CompressionMode.Compress, true);
GZipStrem.Write(byteArray, 0, byteArray.Length);
GZipStrem.Flush();
strm.Flush();
byte[] ByteArrayToreturn= strm.GetBuffer();
GZipStrem.Close();
strm.Close();
return ByteArrayToreturn;
}
public static byte[] DeCompressBuffer(byte[] byteArray)
{
MemoryStream strm = new MemoryStream(byteArray);
GZipStream GZipStrem = new GZipStream(strm, CompressionMode.Decompress,true);
List<byte> ByteListUncompressedData = new List<byte>();
int bytesRead = GZipStrem.ReadByte();
while (bytesRead != -1)
{
ByteListUncompressedData.Add((byte)bytesRead);
bytesRead = GZipStrem.ReadByte();
}
GZipStrem.Flush();
strm.Flush();
GZipStrem.Close();
strm.Close();
return ByteListUncompressedData.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment