Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# Compress Data: GZIP

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

Compress data. Calling a GZIP method should be simple.

In the .NET Framework, no simple method is available by default. We develop one that compresses any byte array in memory and returns that GZIP data. We implement a static compression method.

Byte Array

Example. 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 the File.WriteAllBytes method outputs the data to the file compress.gz.

Convert String, Byte ArrayMemoryStream

C# program that implements Compress method

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();

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

Does it work? Yes—I have 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. The result was the original 10,000 character string.

Decompress. What should you do if you want to decompress a file in memory? You should visit the appropriate article on this site. And then you should rigorously test your program to make sure it works.

Decompress GZIP

Summary. 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 faster than using an external program (7-Zip) but the compression ratio is poorer.

7-Zip Executable