
GZipStream compresses data. It lets you save 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.

Here we will use the .NET Framework's built-in methods in the C# language. It provides these new methods because compression and decompression using GZIP is an extremely common problem in any programming language, and is widely used on the Internet. The GZIP compression algorithm is used on many web pages to boost performance. This method shows the use of GZIP on a string.
This C# example program uses GZipStream to compress a file. It requires System.IO.Compression.
Program that compresses [C#]
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
{
// Couldn't 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.Overview. 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. Remember to add System.IO.Compression at the top. You can find more information about byte arrays here.
Byte ArraySteps. In part A, we combine the Path.GetTempFileName() method and File.WriteAllText. This writes the string to a temporary file.
File HandlingNext steps. In part B, we use FileStream here to read in bytes from the file. The using statement provides effective system resource disposal. Finally in part C, we use a new GZipStream to write the compressed data to the disk. We must combine it with a FileStream. This avoids any problems with clipped data.

This is a tested and reliable methods that I have used in deployed application 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 was tested but had an encoding and decompression problem. Dot Net Perls apologizes for this mistake and thanks Jon for his bug report.

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. For the best compression ratio, going outside the .NET Framework and embedding 7-Zip or another open source program would be excellent.
Compression Tips