Skip to content

Instantly share code, notes, and snippets.

@jhgbrt
Last active February 22, 2023 16:02
Show Gist options
  • Save jhgbrt/11046519 to your computer and use it in GitHub Desktop.
Save jhgbrt/11046519 to your computer and use it in GitHub Desktop.
C# gzipstream
public static byte Compress(string text)
{
using (var ms = new MemoryStream())
{
using (var zip = new GZipStream(ms,
CompressionMode.Compress))
using (var writer = new StreamWriter(zip, Encoding.UTF8))
{
writer.Write(text);
}
return ms.ToArray();
}
}
public static string Decompress(byte[] compressed)
{
using (var ms = new MemoryStream(compressed))
using (var zip = new GZipStream(ms, CompressionMode.Decompress))
using (var sr = new StreamReader(zip, Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment