Home
C#
GZIP Decompress
This page was last reviewed on Mar 24, 2023.
Dot Net Perls
Decompress GZIP. GZIP data is often decompressed before use. A byte array containing GZIP bytes can be translated into one with the original representation of bits.
Method notes. It is possible to use a wrapper method around the GZipStream and other streams. We can implement reliable decompression methods in C# code.
GZIP Compress
This program receives a byte array that contains GZIP data. It transforms the data into a byte array that contains the original representation of bytes.
Note In Decompress() the GZipStream object is first instantiated. The backing store is a MemoryStream wrapped around the GZIP buffer.
Note 2 The second argument to the GZipStream is the CompressionMode.Decompress enumerated constant.
Next A byte array is allocated. The GZIP array is read from the GZipStream and decompressed. This is written to the MemoryStream.
MemoryStream
using System; using System.IO; using System.IO.Compression; class Program { static void Main() { // Open a compressed file on disk. // ... Then decompress it with the method here. // ... Then write the length of each array. byte[] file = File.ReadAllBytes("C:\\perlgzips\\~stat.gz"); byte[] decompressed = Decompress(file); Console.WriteLine(file.Length); Console.WriteLine(decompressed.Length); } static byte[] Decompress(byte[] gzip) { // Create a GZIP stream with decompression mode. // ... Then create a buffer and write into while reading from the GZIP stream. using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress)) { const int size = 4096; byte[] buffer = new byte[size]; using (MemoryStream memory = new MemoryStream()) { int count = 0; do { count = stream.Read(buffer, 0, size); if (count > 0) { memory.Write(buffer, 0, count); } } while (count > 0); return memory.ToArray(); } } } }
9106 36339
Decompress web page. This C# console program decompresses web pages in GZIP format. It uses types from System.IO, System.IO.Compression and System.Net namespaces.
Start When you pass it a URL from the command line, it will download the page in GZIP form.
Next It passes that byte array to the Decompress method. Finally it converts that byte array to a string.
Convert String
WebClient
Info The compressed page from the example required 15,228 bytes. The expanded form required 56,362 bytes (several times larger).
Note Getting the GZIP page with the WebClient would enhance network (and likely overall) performance.
using System; using System.IO; using System.IO.Compression; using System.Net; class Program { static byte[] Decompress(byte[] gzip) { using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress)) { const int size = 4096; byte[] buffer = new byte[size]; using (MemoryStream memory = new MemoryStream()) { int count = 0; do { count = stream.Read(buffer, 0, size); if (count > 0) { memory.Write(buffer, 0, count); } } while (count > 0); return memory.ToArray(); } } } static void Main(string[] args) { try { Console.WriteLine("*** Decompress web page ***"); Console.WriteLine(" Specify file to download"); Console.WriteLine("Downloading: {0}", args[0]); // Download url. using (WebClient client = new WebClient()) { client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; byte[] data = client.DownloadData(args[0]); byte[] decompress = Decompress(data); string text = System.Text.ASCIIEncoding.ASCII.GetString(decompress); Console.WriteLine("Size from network: {0}", data.Length); Console.WriteLine("Size decompressed: {0}", decompress.Length); Console.WriteLine("First chars: {0}", text.Substring(0, 5)); } } finally { Console.WriteLine("[Done]"); } } }
*** Decompress web page *** Specify file to download Downloading: http://en.wikipedia.org/ Size from network: 15228 Size decompressed: 56362 First chars: <!DOC [Done]
Summary. We decompressed an array of GZIP bytes into an array of the original bytes. The C# method shown receives a GZIP byte array and returns the original byte array.
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.