C# Compress Data: GZIP

GZIP compression

You want a simple method that compresses any byte array in memory and returns that GZIP data. By using the MemoryStream and GZipStream classes, we can implement a static compression method.

Implement Compress

To begin, you will need the System.IO, System.IO.Compression, and System.Text namespaces. First, a new string is created with 10,000 X characters; this is converted to a byte array in GetBytes. Next, the Compress method is invoked: this method creates a MemoryStream instance and a GZipStream instance and then writes the GZipStream, which uses the MemoryStream as a buffer.

Then, the MemoryStream is converted to a byte[] array. Finally in this program, the File.WriteAllBytes method outputs the data to the file compress.gz.

Convert String to Byte Array MemoryStream Use

This C# example program compresses a byte array with GZipStream.

Program that implements Compress method [C#]

using System.IO;
using System.IO.Compression;
using System.Text;

class Program
{
    static void Main()
    {
	// Convert 10000 character string to byte array.
	byte[] text = Encoding.ASCII.GetBytes(new string('X', 10000));

	// Use compress method.
	byte[] compress = Compress(text);

	// Write compressed data.
	File.WriteAllBytes("compress.gz", compress);
    }

    /// <summary>
    /// Compresses byte array to new byte array.
    /// </summary>
    public static byte[] Compress(byte[] raw)
    {
	using (MemoryStream memory = new MemoryStream())
	{
	    using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true))
	    {
		gzip.Write(raw, 0, raw.Length);
	    }
	    return memory.ToArray();
	}
    }
}

Result
    1. compress.gz is written to the disk at 213 bytes.
    2. Decompress compress.gz and it is 10000 bytes.

Does it work? Dot Net Perls has published some incorrect GZIP code in the past, so I was hoping to get this one correct. To test, I used the 7-Zip program to extract the compress.gz file, and the result was the original 10,000 character string. So yes, this does work at least in this test.

Decompress

Question and answer

What should you do if you want to decompress a file in memory? You should visit the appropriate article on Dot Net Perls and then rigorously test your program to make sure it works correctly.

Decompress GZIP

Summary

The C# programming language

We demonstrated a method that effectively compresses any data stored in a byte[] array in memory. The code was tested in the .NET 4.0 Framework but will also work with earlier versions such as .NET 3.5. Typically, compressing files in memory in this way is much faster than using an external program such as 7-Zip, but the compression ratio may be poorer.

7-Zip Executable Tutorial Compression Tips
.NET