Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior .Net Consultant and Architect at Sela Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft. Gil is a DZone MVB and is not an employee of DZone and has posted 98 posts at DZone. You can read more from them at their website. View Full User Profile

Back to Basics – Zip in .NET

02.09.2009
Email
Views: 1660
  • submit to reddit

In today’s post I’m going to share a problem I solved this week. The solution was to use the framework’s System.IO.Compression namespace and the GZipStream object.

The Problem

In my current project we save Xml data in our database. The field to save the data was of type varchar(6000). This way of saving the data raised a problem of big Xml data (over 8000 kb for every Xml data) which were saved in the database and for the long run could raise space and performance problems.

The Solution

Use the compression abilities of .NET framework, compress the Xml data and save the data in a binary form. We needed to change the field type in the database to binary type and compress the Xml data before inserting it to the database. After the binary data was retrieved from the database a reverse process of decompress returns the original Xml string.

The Code

I first built a console application to write the code and test it. Then, I wired the zip and unzip methods I wrote to the part that needed the compression. The following code is the console application’s zip and unzip methods I used to compress the Xml data.

static void Main(string[] args) 
{
string data = "<Root><Child></Child>data1<Child>data2</Child><Child>data3</Child><Child>data4</Child><Child>data5</Child></Root>";
Console.WriteLine(data);

byte[] zipped = ZipDocumentData(data);
Console.WriteLine(Encoding.UTF8.GetString(zipped));

data = UnZipDocumentData(zipped);
Console.WriteLine(data);

Console.Read();
}

private static byte[] ZipDocumentData(string documentData)
{
byte[] byteArray = Encoding.UTF8.GetBytes(documentData);
string result = string.Empty;

using (MemoryStream ms = new MemoryStream())
{
using (GZipStream stream = new GZipStream(ms, CompressionMode.Compress))
{
//Compress
stream.Write(byteArray, 0, byteArray.Length);
}
return ms.ToArray();
}
}

private static string UnZipDocumentData(byte[] zippedDocumentData)
{
string result = string.Empty;

//Prepare for decompress
using (MemoryStream ms = new MemoryStream(zippedDocumentData))
{
using (GZipStream stream = new GZipStream(ms, CompressionMode.Decompress))
{
//Reset variable to collect uncompressed result
byte[] byteArray = new byte[4096];

//Decompress
int rByte = stream.Read(byteArray, 0, byteArray.Length);

result = Encoding.UTF8.GetString(byteArray);
}
}
return result;
}

Some things that should be concerned if you are going to use this code:

  • The encoding of the strings I use are in UTF8 format. If you use other formats you should change the Encoding.UTF8 code to the format you use.
  • In the decompress process I use a fixed array of 4096 bytes. This is only for the testing application in the real method I save the original size of the array.

Summary

Lets sum up, I used a compression method to compress Xml data in the database. I showed the code to do that using the GZipStream object which is part of System.IO.Compression namespace. I hope the code will help you when you’ll ever need to compress data.

 

References
Published at DZone with permission of Gil Fink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)