且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

真正简单的短字符串压缩

更新时间:2022-06-23 05:29:43

我认为这里的关键问题是为什么要压缩网址?

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

尝试缩短地址栏的长网址?

更好地将原始URL存储在某个地方(数据库,文本文件...)和非域部分的哈希码(MD5很好)。然后,您可以有一个简单的页面(或一些HTTPModule,如果你感觉闪光)读取MD5和查找真实的URL。这是TinyURL和其他人的工作方式。

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.

例如:

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

可能会被短接至: / p>

Could be shorted to:

http://mydomain.com/2d4f1c8a

使用压缩库无法使用。字符串将被压缩成一个较短的二进制表示,但是将它转换回一个需要作为URL的一部分有效的字符串(例如Base64)将会抵消从压缩中获得的任何好处。

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.

在内存或磁盘上存储大量网址?

使用System.IO中的内置压缩库。压缩或ZLib库,这是简单和令人难以置信的好。因为你将存储二进制数据,压缩的输出将是原样。您需要将其解压缩以将其用作网址。

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.