How to compress and decompress strings in C#

By FoxLearn 12/30/2024 3:16:32 AM   122
Compressing string data using compression algorithms like GZip and Brotli can help reduce memory payload and improve application performance.

In this article, we’ll explore how to implement string compression and decompression using GZip and Brotli in C#.

To begin, create a new .NET Core console application in Visual Studio 2022. Follow these steps:

  1. Launch Visual Studio 2022 and select "Create a new project."
  2. Choose Console App and click Next.
  3. Configure your project by setting a name, location, and selecting .NET 6.0 or later.
  4. Click Create to finish the setup.

Next, install BenchmarkDotNet via NuGet to help measure the performance of different compression algorithms. You can install it either through the NuGet Package Manager or by running the following command in the Package Manager Console:

Install-Package BenchmarkDotNet

Compression and Decompression with GZip

In C#, System.IO.Compression provides GZipStream and BrotliStream for string compression.

Compressing with GZip

public static byte[] Compress(byte[] bytes)
{
    using (var memoryStream = new MemoryStream())
    {
        using (var gzipStream = new GZipStream(memoryStream, CompressionLevel.Optimal))
        {
            gzipStream.Write(bytes, 0, bytes.Length);
        }
        return memoryStream.ToArray();
    }
}

Decompressing with GZip

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

Here’s how to compress and decompress a string with GZip:

string originalString = "GZip compression is widely used in many applications to reduce the size of data for storage or transmission. It is an efficient way to save bandwidth and storage space.";
byte[] dataToCompress = Encoding.UTF8.GetBytes(originalString);
byte[] compressedData = GZipCompressor.Compress(dataToCompress);
Console.WriteLine("Length of compressed string: " + compressedData.Length);
byte[] decompressedData = GZipCompressor.Decompress(compressedData);
Console.WriteLine("Length of decompressed string: " + decompressedData.Length);

Compression and Decompression with Brotli

The Brotli algorithm generally provides better compression ratios than GZip.

Compressing with Brotli

public static byte[] Compress(byte[] bytes)
{
    using (var memoryStream = new MemoryStream())
    {
        using (var brotliStream = new BrotliStream(memoryStream, CompressionLevel.Optimal))
        {
            brotliStream.Write(bytes, 0, bytes.Length);
        }
        return memoryStream.ToArray();
    }
}

Decompressing with Brotli

public static byte[] Decompress(byte[] bytes)
{
    using (var memoryStream = new MemoryStream(bytes))
    {
        using (var outputStream = new MemoryStream())
        {
            using (var brotliStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
            {
                brotliStream.CopyTo(outputStream);
            }
            return outputStream.ToArray();
        }
    }
}

Running Brotli Compression

byte[] dataToCompress = Encoding.UTF8.GetBytes(originalString);
byte[] compressedData = BrotliCompressor.Compress(dataToCompress);
Console.WriteLine("Length of compressed string: " + compressedData.Length);
byte[] decompressedData = BrotliCompressor.Decompress(compressedData);
Console.WriteLine("Length of decompressed string: " + decompressedData.Length);

Asynchronous Compression and Decompression

Both GZip and Brotli support asynchronous compression and decompression.

Asynchronous GZip Compression

public static async Task<byte[]> CompressAsync(byte[] bytes)
{
    using (var memoryStream = new MemoryStream())
    {
        using (var gzipStream = new GZipStream(memoryStream, CompressionLevel.Optimal))
        {
            await gzipStream.WriteAsync(bytes, 0, bytes.Length);
        }
        return memoryStream.ToArray();
    }
}

Asynchronous Brotli Compression

public static async Task<byte[]> CompressAsync(byte[] bytes)
{
    using (var memoryStream = new MemoryStream())
    {
        using (var brotliStream = new BrotliStream(memoryStream, CompressionLevel.Optimal))
        {
            await brotliStream.WriteAsync(bytes, 0, bytes.Length);
        }
        return memoryStream.ToArray();
    }
}

Benchmarking Compression Algorithms

To evaluate the performance of GZip and Brotli, we use BenchmarkDotNet.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using System;
using System.Text;

namespace CompressionBenchmark
{
    [MemoryDiagnoser]
    [Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
    [RankColumn]
    public class BenchmarkCompression
    {
        string originalString = "This example demonstrates comparing the GZip and Brotli compression algorithms using BenchmarkDotNet in C#. The goal is to test and evaluate the performance of each algorithm in terms of speed and compression ratio.";

        [Benchmark]
        public void GZipCompress()
        {
            byte[] dataToCompress = Encoding.UTF8.GetBytes(originalString);
            byte[] compressedData = GZipCompressor.Compress(dataToCompress);
        }

        [Benchmark]
        public void BrotliCompress()
        {
            byte[] dataToCompress = Encoding.UTF8.GetBytes(originalString);
            byte[] compressedData = BrotliCompressor.Compress(dataToCompress);
        }
    }
}

When you run the benchmark, the results will show the performance difference between GZip and Brotli. GZip is faster, but Brotli achieves a higher compression ratio. This means that although Brotli takes longer, it compresses data more efficiently, which can be useful for reducing network bandwidth or storage space.

When optimizing string data in .NET Core applications, both GZip and Brotli offer valuable compression methods. GZip is faster but provides a lower compression ratio, while Brotli offers better compression but at the cost of performance.