GZip compression and decompression in C#

The snippets below illustrate GZip compression and decompression using the GZipStream classes.

GZip class

using System;
using System.IO;
using System.IO.Compression;

namespace GZipExample
{
	public class GZip
	{
		public GZip ()
		{
		}

		public static byte[] Compress (byte[] data)
		{
			using (MemoryStream ms = new MemoryStream()) {
				using (GZipStream gzStream = new GZipStream(ms, CompressionMode.Compress, true)) {
					gzStream.Write (data, 0, data.Length);
				}
				return ms.ToArray ();
			}
		}

		public static byte[] Decompress (byte[] data)
		{
			using (GZipStream gzStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress, true)) {
				const int bufferSize = 4096;
				int bytesRead = 0;

				byte[] buffer = new byte[bufferSize];

				using (MemoryStream ms = new MemoryStream()) {
					while((bytesRead = gzStream.Read(buffer, 0, bufferSize)) > 0){
						ms.Write(buffer, 0, bytesRead);
					}
					return ms.ToArray();
				}
			}
		}

	}
}

The main program

using System;
using System.Text;

namespace GZipExample
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			string data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
				"Etiam malesuada lectus id lectus scelerisque pulvinar. " +
				"Etiam ut turpis libero, at condimentum turpis. " +
				"Integer in purus ut purus eleifend posuere vel ut nibh. " +
				"Praesent interdum rhoncus nisl, id consectetur orci luctus quis. " +
				"Aliquam molestie ornare nisi a sollicitudin. " +
				"Integer nec erat ut ipsum placerat tristique. " +
				"In pulvinar sapien at erat ultricies iaculis. " +
				"Maecenas mollis est sit amet urna scelerisque eu accumsan lacus rutrum. " +
				"Donec purus metus, blandit dignissim varius id, porttitor sed metus. " +
				"Cras nec sapien vitae sem porta dapibus eget sit amet eros. " +
				"Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. " +
				"Suspendisse potenti. Sed id diam eget nisi laoreet scelerisque vitae vulputate lacus. " +
				"Phasellus quis leo sapien, id hendrerit mi. Morbi feugiat mollis nisl id egestas.";

			UTF8Encoding encoding = new UTF8Encoding();
			byte[] dataBytes = encoding.GetBytes(data);

			Console.WriteLine("Data length: " + dataBytes.Length);

			byte[] compressedData = GZip.Compress(dataBytes);

			Console.WriteLine("Compressed length: " + compressedData.Length);

			byte[] decompressedData = GZip.Decompress(compressedData);

			Console.WriteLine("Decompressed length: " + decompressedData.Length);
			Console.WriteLine("Decompressed string: " + encoding.GetString(decompressedData));
			Console.ReadLine();
		}
	}
}

Output

Data length: 877
Compressed length: 486
Decompressed length: 877
Decompressed string: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam malesuada lectus id lectus scelerisque pulvinar. Etiam ut turpis libero, at condimentum turpis. Integer in purus ut purus eleifend posuere vel ut nibh. Praesent interdum rhoncus nisl, id consectetur orci luctus quis. Aliquam molestie ornare nisi a sollicitudin. Integer nec erat ut ipsum placerat tristique. In pulvinar sapien at erat ultricies iaculis. Maecenas mollis est sit amet urna scelerisque eu accumsan lacus rutrum. Donec purus metus, blandit dignissim varius id, porttitor sed metus. Cras nec sapien vitae sem porta dapibus eget sit amet eros. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse potenti. Sed id diam eget nisi laoreet scelerisque vitae vulputate lacus. Phasellus quis leo sapien, id hendrerit mi. Morbi feugiat mollis nisl id egestas.

You may download the solution file from here.

This entry was posted in C# and tagged , , , , , , , , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?