Saturday, October 18, 2008

How to Compress & Decompress Strings in C# ?

This article show how to compress strings in C#. Dotnet Framework provides classes to compress the streams which can be found in 'System.IO.Compression'.The class i'm going to use is 'GZipStream'.Basically this class compresses streams , one can be use this class to compress even files.But here i'm using to compress string.

As i said class 'GZipStream' provides methods to compress streams, i'm converting my string to stream first then compressing the resulting stream.
Code looks like this
//
For Compression
public string Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}



For decompression
public string Decompress(string compressedText)
{
byte[] gzBuffer = Convert.FromBase64String(compressedText);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}

Note:The compression rate will be higher for larger strings.As the number character in string increases the rate of compression will be increased.

1 comment:

Piloki Homestay Project said...

Thanks, this is great! But why is it necessary to make the buffer with 4 extra bytes?