C Programming Tutorials and advice
CProgrammingTrends News Archives About Us Feedback

Recent Articles


Nokia Eases the pain for C and Linux Developers
For mobile application developers and those creating LBS apps this is huge!

Generate Unique Strings & Numbers In C#
The System.Guid is used whenever we need to generate a unique key, but it is very long. That's in many cases not an issue, but in a web scenario where it is part of the URL we need to use its string representation which...

C# - Generic Plug-in Application
At work, we are about to build a new internal tool for administration of different systems. We developers need certain applications and the sales department...

C# - Screen Scraping
Some say that screen scraping is a lost art because it is no longer an advanced discipline. That may be right, but there are different ways of doing it. Here are some different ways that all are perfectly acceptable...

C# Stock Quote Class
A couple of months ago one of my readers asked me to build a stock quote class that would automatically update the quote. I forgot about it, but then I found the...

Overload Operators The Right Way In C#
It's a good rule of thumb to overload the equality operators on classes. That ensures a correct comparison between to class instances of the same type. If you don't, .NET automatically uses reflection and that is way...

C#: Realtime Currency Exchange Class
Soon, I'm facing a new project where the need for up-to-date currency exchanges is crucial. This is not the first time I've done this, but I wanted to make...

Validating Strong Passwords in C# and ASP.NET
It's always a good idea to have a password policy when creating new applications.


05.03.07


Compress & Decompress Strings In C#


By Mads Kristensen

A couple of days ago, I had to store some very big strings in an XML file.

To keep the file size down I wanted to compress the strings using a GZipStream and then decompress them later when needed.

I modified the methods from Papenoo pa so they handled strings in stead of byte arrays.

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

publicstaticstring 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;
MemoryStream outStream =new MemoryStream();

byte[] compressed =newbyte[ms.Length];
ms.Read(compressed, 0, compressed.Length);

Low Rate eCommerce & Retail Plans

byte[] gzBuffer =newbyte[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);
}

publicstaticstring 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 =newbyte[msgLength];

ms.Position = 0;
using (GZipStream zip =new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}

return Encoding.UTF8.GetString(buffer);
}
}


The strings need to be longer than 3-400 characters; otherwise the compression rate is not good enough.

About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

About CProgrammingTrends
A collection of articles and tutorials designed to help C and C variant programmers in their programming work. C Programming Tutorials and advice

CProgrammingTrends is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagmentNews.com


-- CProgrammingTrends is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2007 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal


archives | advertising info | news headlines | free newsletters | comments/feedback | submit article


CProgrammingTrends Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact