Created
December 7, 2022 08:29
-
-
Save kid-cavaquinho/f082baa7245c4b34ae3e151e3c9070d7 to your computer and use it in GitHub Desktop.
Gzip compression/decompression
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 class Gzip | |
| { | |
| private static async Task<byte[]> DecompressBytesAsync(byte[] bytes, CancellationToken cancel = default) | |
| { | |
| using var inputStream = new MemoryStream(bytes); | |
| using var outputStream = new MemoryStream(); | |
| await using (var compressionStream = new GZipStream(inputStream, CompressionMode.Decompress)) | |
| { | |
| await compressionStream.CopyToAsync(outputStream, cancel); | |
| } | |
| return outputStream.ToArray(); | |
| } | |
| public static async Task<byte[]> CompressBytesAsync(byte[] bytes, CancellationToken cancel = default) | |
| { | |
| using var outputStream = new MemoryStream(); | |
| await using (var compressionStream = new GZipStream(outputStream, CompressionLevel.Optimal)) | |
| { | |
| await compressionStream.WriteAsync(bytes, 0, bytes.Length, cancel); | |
| } | |
| return outputStream.ToArray(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment