.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple


C#: .NET: Compression
GZIP compression

GZipStream compresses data. It saves data efficiently—such as in compressed log files. We develop a utility method in the C# language that uses the System.IO.Compression namespace. It creates GZIP files. It writes them to the disk.


Note

Example. Here we will use the .NET Framework's built-in methods. It provides these new methods because compression and decompression using GZIP is an extremely common problem in any programming language. GZIP is widely used on the Internet.

Note:The GZIP compression algorithm is used on many web pages to boost performance. This method shows the use of GZIP on a string.

C# program that compresses

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

class Program
{
    static void Main()
    {
	try
	{
	    // 1.
	    // Starting file is 26,747 bytes.
	    string anyString = File.ReadAllText("TextFile1.txt");

	    // 2.
	    // Output file is 7,388 bytes.
	    CompressStringToFile("new.gz", anyString);
	}
	catch
	{
	    // Could not compress.
	}
    }

    public static void CompressStringToFile(string fileName, string value)
    {
	// A.
	// Write string to temporary file.
	string temp = Path.GetTempFileName();
	File.WriteAllText(temp, value);

	// B.
	// Read file into byte array buffer.
	byte[] b;
	using (FileStream f = new FileStream(temp, FileMode.Open))
	{
	    b = new byte[f.Length];
	    f.Read(b, 0, (int)f.Length);
	}

	// C.
	// Use GZipStream to write compressed bytes to target file.
	using (FileStream f2 = new FileStream(fileName, FileMode.Create))
	using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
	{
	    gz.Write(b, 0, b.Length);
	}
    }
}

Result
    Starting file is 26,747 bytes.
    Output file is 7,388 bytes.
Squares

The method compresses strings to disk. GZipStream requires bytes and this example uses a byte array. We can use the File static methods to write to temporary files. You see several useful framework methods.

Note:Remember to add System.IO.Compression at the top. You can find more information about byte arrays here.

Byte Array: Memory Usage, Read All Bytes

In part A, we combine the Path.GetTempFileName() method and File.WriteAllText. This writes the string to a temporary file. In part B, we use FileStream here to read in bytes from the file.

File Handling

Using:The using statement provides effective system resource disposal. More examples are available.

Using

Finally:In part C, we use a new GZipStream to write the compressed data to the disk. We must combine it with a FileStream.


Exclamation mark

Reliable. This is a tested and reliable method that I have used in deployed applications with no problems. It however can be slow so if you are working on a program with extremely strict performance requirements, don't use this code.

Note:This article used to have an example that was incorrect. It had an encoding problem. I thank Jon for his bug report.


C# programming language

Summary. We saw an example of using GZipStream to compress to a file with an underlying FileStream. More complex methods are available for compression and this example alone does not meet most requirements.

Tip:For the best compression ratio, going outside the .NET Framework and embedding 7-Zip is often helpful.