PROWAREtech

articles » current » dot-net » compression-gzip

.NET: Using GZip to Compress and Decompress Data

How to use the GZipStream class to compress and decompress bytes, files and streams of data in C#.

.NET makes compressing and decompressing data with GZip incredibly easy. Compress and decompress byte arrays for simplicity or work with files and streams for purposes of efficiency.

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

This library will compress streams from the position they are at, so put the input stream at position 0 if wanting to compress the whole stream.


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

namespace Utilities
{
	public class GZip
	{
		private static CompressionLevel GetCompressionLevel()
		{
			if(Enum.IsDefined(typeof(CompressionLevel), 3)) // NOTE: CompressionLevel.SmallestSize == 3 is not supported in .NET Core 3.1 but is in .NET 6
			{
				return (CompressionLevel)3;
			}
			return CompressionLevel.Optimal;
		}
		public static byte[] CompressBytes(byte[] bytes) => CompressBytesAsync(bytes).GetAwaiter().GetResult();
		public static async Task<byte[]> CompressBytesAsync(byte[] bytes, CancellationToken cancel = default(CancellationToken))
		{
			using (var outputStream = new MemoryStream())
			{
				using (var compressionStream = new GZipStream(outputStream, GetCompressionLevel()))
				{
					await compressionStream.WriteAsync(bytes, 0, bytes.Length, cancel);
				}
				return outputStream.ToArray();
			}
		}

		public static void CompressFile(string originalFileName, string compressedFileName) => CompressFileAsync(originalFileName, compressedFileName).GetAwaiter().GetResult();
		public static async Task CompressFileAsync(string originalFileName, string compressedFileName, CancellationToken cancel = default(CancellationToken))
		{
			using (FileStream originalStream = File.Open(originalFileName, FileMode.Open))
			{
				using (FileStream compressedStream = File.Create(compressedFileName))
				{
					await CompressStreamAsync(originalStream, compressedStream, cancel);
				}
			}
		}

		public static void CompressStream(Stream originalStream, Stream compressedStream) => CompressStreamAsync(originalStream, compressedStream).GetAwaiter().GetResult();
		public static async Task CompressStreamAsync(Stream originalStream, Stream compressedStream, CancellationToken cancel = default(CancellationToken))
		{
			using (var compressor = new GZipStream(compressedStream, GetCompressionLevel()))
			{
				await originalStream.CopyToAsync(compressor, cancel);
			}
		}

		public static byte[] DecompressBytes(byte[] bytes) => DecompressBytesAsync(bytes).GetAwaiter().GetResult();
		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();
				}
			}
		}
		public static void DecompressFile(string compressedFileName, string outputFileName) => DecompressFileAsync(compressedFileName, outputFileName).GetAwaiter().GetResult();
		public static async Task DecompressFileAsync(string compressedFileName, string outputFileName, CancellationToken cancel = default(CancellationToken))
		{
			using (FileStream compressedFileStream = File.Open(compressedFileName, FileMode.Open))
			{
				using (FileStream outputFileStream = File.Create(outputFileName))
				{
					await DecompressStreamAsync(compressedFileStream, outputFileStream, cancel);
				}
			}
		}
		public static void DecompressStream(Stream compressedStream, Stream outputStream) => DecompressStreamAsync(compressedStream, outputStream).GetAwaiter().GetResult();
		public static async Task DecompressStreamAsync(Stream compressedStream, Stream outputStream, CancellationToken cancel = default(CancellationToken))
		{
			using (var decompressor = new GZipStream(compressedStream, CompressionMode.Decompress))
			{
				await decompressor.CopyToAsync(outputStream, cancel);
			}
		}
	}
}

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.GZip.CompressBytes(bytes);

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

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

bytes = Utilities.GZip.DecompressBytes(bytes);

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

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

Coding Video

https://youtu.be/Xb2PSCkkaaE


This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE