Home
Map
GZIP Compress ExampleCompress files with GZipStream, BrotliStream and MemoryStream. Write the compressed data to the disk.
C#
This page was last reviewed on Mar 24, 2023.
Compress data. Calling a GZIP compression method should be simple. We develop a method that compresses any byte array in memory and returns that GZIP data.
Method notes. We implement a static compression method. We can handle GZIP in older .NET versions, and Brotli in newer versions like .NET 5.
GZIP Decompress
File
GZIP example. Please notice the namespaces at the top. First, a new string is created with 10,000 X characters. This is converted to a byte array in GetBytes.
byte Array
Info Compress() creates a MemoryStream and GZipStream. It then writes to the GZipStream, which uses 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
MemoryStream
Detail To test, I used the 7-Zip program to extract the compress.gz file. The result was the original 10,000 character string.
using System; using System.IO; using System.IO.Compression; using System.Text; class Program { 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. File.WriteAllBytes(@"C:\programs\compress.gz", compress); Console.WriteLine("DONE"); } /// <summary> /// Compresses byte array to new byte array. /// </summary> public static byte[] Compress(byte[] raw) { using (MemoryStream memory = new MemoryStream()) { using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true)) { gzip.Write(raw, 0, raw.Length); } return memory.ToArray(); } } }
DONE
Brotli example. This code compressed both in Brotli with BrotliStream, and GZIP with GZipStream. Brotli is a newer compression format that works better than GZIP on web pages.
Part 1 Read in a file to compress. Please change the path name to one that exists on you system.
Part 2 Call the 2 compression methods, and write the byte arrays returned to disk. These are the final compressed files.
Tip If you are reading this page, this Brotli compression method must have worked correctly.
Detail BrotliStream can only be used on newer versions of .NET, as it is a newer compression format.
using System; using System.IO; using System.IO.Compression; class Program { static void Main() { // Part 1: read in file. var data = File.ReadAllBytes(@"C:\stage-perls\_sitemap"); // Part 2: write out compressed files. File.WriteAllBytes(@"C:\programs\brotli-out", CompressBrotli(data)); File.WriteAllBytes(@"C:\programs\gzip-out", CompressGZip(data)); Console.WriteLine("DONE"); } static byte[] CompressBrotli(byte[] raw) { using (MemoryStream memory = new MemoryStream()) { using (BrotliStream brotli = new BrotliStream(memory, CompressionLevel.Optimal)) { brotli.Write(raw, 0, raw.Length); } return memory.ToArray(); } } static byte[] CompressGZip(byte[] raw) { using (MemoryStream memory = new MemoryStream()) { using (GZipStream gzip = new GZipStream(memory, CompressionLevel.Optimal)) { gzip.Write(raw, 0, raw.Length); } return memory.ToArray(); } } }
DONE
A summary. We wrote a method that effectively compresses any data stored in a byte array in memory. GZIP and Brotli can be used to compress the data.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 24, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.