Created
January 29, 2024 14:13
-
-
Save unitycoder/41fce46cf6ee4f7e2594b926928106be to your computer and use it in GitHub Desktop.
GZipCompress c#
This file contains hidden or 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
| 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