Really simple short string compression

后端 未结 9 1293
感动是毒
感动是毒 2020-12-01 16:21

Is there a really simple compression technique for strings up to about 255 characters in length (yes, I\'m compressing URLs)?

I am not concerned with the strength o

9条回答
  •  离开以前
    2020-12-01 16:44

    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.

提交回复
热议问题