Skip to content

Instantly share code, notes, and snippets.

@zhenlinyang
Created December 6, 2016 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhenlinyang/803ef2ee2f54648d4680d22326106f9d to your computer and use it in GitHub Desktop.
Save zhenlinyang/803ef2ee2f54648d4680d22326106f9d to your computer and use it in GitHub Desktop.
CSharp GZip
public static class ZipMng
{
public static byte[] Compress(byte[] originalData)
{
using (MemoryStream outms = new MemoryStream())
{
using (MemoryStream inms = new MemoryStream(originalData))
{
using (GZipStream gzs = new GZipStream(outms, CompressionMode.Compress))
{
byte[] buffer = new byte[81920];
int read = 0;
while ((read = inms.Read(buffer, 0, buffer.Length)) != 0)
{
gzs.Write(buffer, 0, read);
}
}
}
return outms.ToArray();
}
}
public static byte[] Decompress(byte[] compressedData)
{
using (MemoryStream outms = new MemoryStream())
{
using (MemoryStream inms = new MemoryStream(compressedData))
{
using (GZipStream gzs = new GZipStream(inms, CompressionMode.Decompress))
{
byte[] buffer = new byte[81920];
int read = 0;
while ((read = gzs.Read(buffer, 0, buffer.Length)) != 0)
{
outms.Write(buffer, 0, read);
}
}
}
return outms.ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment