TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# GZipStream Example

This C# example program uses GZipStream to compress a file. It requires System.IO.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.

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.

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.

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.

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf