C# Compress Data: GZIP

Compress a byte array with GZipStream and MemoryStream. Write the compressed data to the disk.

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.Decompress GZIPFile

Tip: 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

Example. 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.Byte Array

Compress: This method creates MemoryStream and GZipStream instances 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(); } } } Output 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.

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.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to