TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# GZipStream Example (DeflateStream)

Use GZipStream from System.IO.Compression to compress a file. Handle DeflateStream as well.
GZipStream. This class compresses data. It saves data efficiently—such as in compressed log files. GZIP has a file header, while DEFLATE does not.Compress Data
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.
GZipStream example. We use the .NET Framework's built-in methods. Compression and decompression using GZIP is common 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.

Strings

Part A: We use Path.GetTempFileName and File.WriteAllText. This writes the string to a temporary file.

Part B: We use FileStream here to read in bytes from the file. The using statement provides effective system resource disposal.

UsingByte Array

Part C: We use a new GZipStream to write the compressed data to the disk. We must combine it with a FileStream.

C# program that compresses using System.IO; using System.IO.Compression; using System.Text; class Program { static void Main() { try { // Starting file is 26,747 bytes. string anyString = File.ReadAllText("TextFile1.txt"); // Output file is 7,388 bytes. CompressStringToFile("new.gz", anyString); } catch { // Could not compress. } } public static void CompressStringToFile(string fileName, string value) { // Part A: write string to temporary file. string temp = Path.GetTempFileName(); File.WriteAllText(temp, value); // Part 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); } // Part 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); } } } Output Starting file is 26,747 bytes. Output file is 7,388 bytes.
DeflateStream. With DeflateStream we can output a deflate file. A GZIP file contains a DEFLATE body. With GZIP we have an additional header.

So: We can use DeflateStream to generate the DEFLATE body with no header. This may be useful in some situations.

C# program that uses DeflateStream using System.IO; using System.IO.Compression; class Program { static void Main() { CompressWithDeflate("C:\\programs\\example.txt", "C:\\programs\\deflate-output"); } static void CompressWithDeflate(string inputPath, string outputPath) { // Read in input file. byte[] b = File.ReadAllBytes(inputPath); // Write compressed data with DeflateStream. using (FileStream f2 = new FileStream(outputPath, FileMode.Create)) using (DeflateStream deflate = new DeflateStream(f2, CompressionMode.Compress, false)) { deflate.Write(b, 0, b.Length); } } }
Notes, reliable. CompressStringToFile is tested and reliable. I have used it in deployed applications. It however can be slow—caching, or other optimizations may be needed.Memoization

Note: This article used to have an example that was incorrect. It had an encoding problem. Jon wrote in with a fix.

A summary. We used GZipStream to compress to a file with an underlying FileStream. More complex methods are available for compression. This example alone does not meet most requirements.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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