Using GZIP (GZipStream) to Compress / Decompress Data in .NET

See related: .NET examples

.NET makes compressing and decompressing data with GZip incredibly easy. In this example, there are four methods/functions that take a byte array and either compresses or decompresses it returning the output as a byte array. All Stream objects can be converted to byte arrays.

This library is compatible with Blazor WASM.

See related articles: Compress Data with BrotliStream, Compress and Upload Files with Blazor WASM, Compress HTTPS Blazor WASM WebAPI Output and Compress HTTPS .NET Core WebAPI Output

// GZipCompressor.cs
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;

namespace Utilities
{
	public class GZipCompressor
	{
		public static byte[] CompressBytes(byte[] bytes)
		{
			using (var outputStream = new MemoryStream())
			{
				using (var compressionStream = new GZipStream(outputStream, CompressionLevel.Optimal))
				{
					compressionStream.Write(bytes, 0, bytes.Length);
				}
				return outputStream.ToArray();
			}
		}

		public static byte[] DecompressBytes(byte[] bytes)
		{
			using (var inputStream = new MemoryStream(bytes))
			{
				using (var outputStream = new MemoryStream())
				{
					using (var compressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
					{
						compressionStream.CopyTo(outputStream);
					}
					return outputStream.ToArray();
				}
			}
		}

		public static async Task<byte[]> CompressBytesAsync(byte[] bytes, CancellationToken cancel = default(CancellationToken))
		{
			using (var outputStream = new MemoryStream())
			{
				using (var compressionStream = new GZipStream(outputStream, CompressionLevel.Optimal))
				{
					await compressionStream.WriteAsync(bytes, 0, bytes.Length, cancel);
				}
				return outputStream.ToArray();
			}
		}

		public static async Task<byte[]> DecompressBytesAsync(byte[] bytes, CancellationToken cancel = default(CancellationToken))
		{
			using (var inputStream = new MemoryStream(bytes))
			{
				using (var outputStream = new MemoryStream())
				{
					using (var compressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
					{
						await compressionStream.CopyToAsync(outputStream, cancel);
					}
					return outputStream.ToArray();
				}
			}
		}
	}
}

It is easy to use this code on files to compress and decompress them.

var bytes = System.IO.File.ReadAllBytes("TEMP.txt"); // MAKE SURE TEMP.txt EXISTS!!!

Console.WriteLine("INPUT FILE IS " + bytes.Length + " bytes");

bytes = Utilities.GZipCompressor.CompressBytes(bytes);

Console.WriteLine("COMPRESSED FILE IS " + bytes.Length + " bytes");

System.IO.File.WriteAllBytes("TEMP.gzip", bytes);

bytes = Utilities.GZipCompressor.DecompressBytes(bytes);

Console.WriteLine("DECOMPRESSED FILE IS " + bytes.Length + " bytes");

System.IO.File.WriteAllBytes("TEMP2.txt", bytes);

Coding Video

https://youtu.be/Xb2PSCkkaaE


Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site.
OK