views:

4079

answers:

8

Does anyone know of a really simple compression technique for strings up to about 255 characters in length (yes I'm compressing urls)?

Not concerned with strength of compression - I am looking for something that performs very well and is quick to implement.

I would like something simpler than SharpZipLib: something that can be implemented with a couple of short methods.

A: 

What's your goal?

peSHIr
Not concerned with strength of compression - I am looking for something that performs very well and is quick to implement. Can you point me to base64?
cbp
Base64 is not going to compress anything :)
Jon Grant
@Jon Grant: Correct. Base64 was a stupid suggestion. Would only work after actually compressing to get something that (perhaps) is smaller, but still ascii. Have removed all trace of the suggestion.
peSHIr
A: 

I would start with trying one of the existing (free or open source) zip libraries, e.g. http://www.icsharpcode.net/OpenSource/SharpZipLib/

Zip should work well for text strings, and I am not sure if it is worth implementing a compression algorithm yourserlf....

Grzenio
+1  A: 

I'd suggest looking in the System.IO.Compression Namespace. There's an article on CodeProject that may help.

Dan Diplo
A: 

Have you tried just using gzip?

No idea if it would work effectively with such short strings, but I'd say its probably your best bet.

Kragen
A: 

The open source library SharpZipLib is easy to use and will provide you with compression tools

Wolfwyrd
+11  A: 

I think the key question here is "Why do you want to compress URLs?"

Trying to shorten long urls for the address bar?

You're better storing the original URL somewhere (database, text file ...) alongside a hashcode of the non-domain part (MD5 is fine). You can then have a simple page (or some HTTPModule if you're feeling flashy) to read the MD5 and lookup the real URL. This is how TinyURL and others work.

For example:

http://mydomain.com/folder1/folder2/page1.aspx

Could be shorted to:

http://mydomain.com/2d4f1c8a

Using a compression library for this will not work. The string will be compressed into a shorter binary representation, but converting this back to a string which needs to be valid as part of a URL (e.g. Base64) will negate any benefit you gained from the compression.

Storing lots of URLs in memory or on disk?

Use the built in compressing library within System.IO.Compression or the ZLib library which is simple and incredibly good. Since you will be storing binary data the compressed output will be fine as-is. You'll need to uncompress it to use it as a URL.

badbod99
That's not an answer to the question. What if you have nowhere to store the hashtable?
endolith
@endolith - The point is string compression will not help you here, only relating it to a hash or similar. See Cheeso's answer for real world example compressions longer and just as long in the original when converted back to valid URLs. You always have "somewhere" to store a hash. Hard code it into your URL redirection code if you really do have "nowhere" to store it!
badbod99
@badbod99: You don't always have somewhere to store a hashtable, and it doesn't always make the URL longer. http://en.wikipedia.org/wiki/Data_URI_scheme, for instance
endolith
Data uri is not any sort of compression, and had nothing to do with shortening urls. In fact data uri is for embedding data in web pages and uses base64, which if you read chesso's answer you will see is much longer. In which case would you not have somewhere to store url/hash code references? If you have a form of compression which will shorten a URL and still be a valid URL please post it as an answer, I'm sure the community will benefit.
badbod99
+1  A: 

As suggested in the accepted answer, Using data compression does not work to shorten URL paths that are already fairly short.

DotNetZip has a DeflateStream class that exposes a static (Shared in VB) CompressString method. It's a one-line way to compress a string using DEFLATE (RFC 1951). The DEFLATE implementation is fully compatible with System.IO.Compression.DeflateStream, but DotNetZip compresses better. Here's how you might use it:

string[] orig = {
    "folder1/folder2/page1.aspx",
    "folderBB/folderAA/page2.aspx",
};
public void Run()
{
    foreach (string s in orig)
    {
        System.Console.WriteLine("original    : {0}", s);
        byte[] compressed = DeflateStream.CompressString(s);
        System.Console.WriteLine("compressed  : {0}", ByteArrayToHexString(compressed));
        string uncompressed = DeflateStream.UncompressString(compressed);
        System.Console.WriteLine("uncompressed: {0}\n", uncompressed);
    }
}

Using that code, here are my test results:

original    : folder1/folder2/page1.aspx
compressed  : 4bcbcf49492d32d44f03d346fa0589e9a9867a89c5051500
uncompressed: folder1/folder2/page1.aspx

original    : folderBB/folderAA/page2.aspx
compressed  : 4bcbcf49492d7272d24f03331c1df50b12d3538df4128b0b2a00
uncompressed: folderBB/folderAA/page2.aspx

So you can see the "compressed" byte array, when represented in hex, is longer than the original, about 2x as long. The reason is that a hex byte is actually 2 ASCII chars.

You could compensate somewhat for that by using base-62, instead of base-16 (hex) to represent the number. In that case a-z and A-Z are also digits, giving you 0-9 (10) + a-z (+26) + A-Z (+26) = 62 total digits. That would shorten the output significantly. I haven't tried that. yet.


EDIT
Ok I tested the Base-62 encoder. It shortens the hex string by about half. I figured it would cut it to 25% (62/16 =~ 4) But I think I am losing something with the discretization. In my tests, the resulting base-62 encoded string is about the same length as the original URL. So, no, using compression and then base-62 encoding is still not a good approach. you really want a hash value.

Cheeso
A: 

You can use deflate algorithm directly, without any headers checksums or footers, as described in this question: Python: Inflate and Deflate implementations

This cuts down a 4100 character URL to 1270 base64 characters, in my test, allowing it to fit inside IE's 2000 limit.

And here's an example of a 4000-character URL, which can't be solved with a hashtable since the applet can exist on any server.

endolith