C# Compress Data: GZIPThis C# example program compresses a byte array with GZipStream.
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
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
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.
/// Compresses byte array to new byte array.
public static byte Compress(byte raw)
using (MemoryStream memory = new MemoryStream())
using (GZipStream gzip = new GZipStream(memory,
gzip.Write(raw, 0, raw.Length);
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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.