Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created January 29, 2024 14:13
Show Gist options
  • Select an option

  • Save unitycoder/41fce46cf6ee4f7e2594b926928106be to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/41fce46cf6ee4f7e2594b926928106be to your computer and use it in GitHub Desktop.
GZipCompress c#
using System.IO.Compression;
public static string GZipCompress(string stringToCompress)
{
byte[] byteArray = Encoding.UTF8.GetBytes(stringToCompress);
using (MemoryStream outputStream = new MemoryStream())
{
using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress))
{
compressionStream.Write(byteArray, 0, byteArray.Length);
}
return Convert.ToBase64String(outputStream.ToArray());
}
}
public static string GZipDecompress(string compressedInput)
{
byte[] compressedData = Convert.FromBase64String(compressedInput);
using (MemoryStream inputStream = new MemoryStream(compressedData))
{
using (GZipStream decompressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(decompressionStream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment