Last active
February 22, 2023 16:02
-
-
Save jhgbrt/11046519 to your computer and use it in GitHub Desktop.
C# gzipstream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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