自己做网站很难,一个网站的建设需要什么,网站建设行业细分,大连旧房翻新装修哪家公司好数据压缩是一个软件开发中的常见需求#xff1a;很多时候需要先将较大的数据进行压缩然后再通过网络等进行传输。在 .NET 中#xff0c;有多个压缩算法供我们选择#xff1a;Deflate、GZip 和 Br 。这些压缩算法都是基于流#xff08;Stream#xff09;的#xff0c;在对… 数据压缩是一个软件开发中的常见需求很多时候需要先将较大的数据进行压缩然后再通过网络等进行传输。在 .NET 中有多个压缩算法供我们选择Deflate、GZip 和 Br 。这些压缩算法都是基于流Stream的在对字符串压缩前需要先将其转换成字节数组。还有一个比较常见的压缩算法是lz-string 。其官方文档在这里https://pieroxy.net/blog/pages/lz-string/index.html压缩后的数据一般是流或者字节数组在压缩字符串的场景下期望的压缩结果大概率也是字符串。常见的字节数组编码方式有以下几种Base16Base62Base64Ascii85不同的压缩算法对应了不同的解压缩算法。同样的不同的编码算法也对应了不同的解码算法。为了降低使用难度可以在压缩结果中将使用的压缩算法和编码算法嵌入这样就可以使用一个解压缩方法去解压任意支持的数据。LuYao.Common基于上述考虑笔者编写了一个名为 StringZipper 的静态类用于辅助进行字符串的压缩和解压缩。该类属于 LuYao.Common 可以在 NuGet 下载https://www.nuget.org/packages/LuYao.Common/StringZipper 的成员public static class StringZipper
{public interface ICompressor{string Identifier { get; }byte[] Compress(string value);string Decompress(byte[] data);}public interface IEncoder{string Identifier { get; }string Encode(byte[] data);byte[] Decode(string value);}public static IEncoder Base16 { get; }public static IEncoder Base62 { get; }public static IEncoder Base64 { get; }public static IEncoder Ascii85 { get; }public static ICompressor LzString { get; }public static ICompressor Deflate { get; }public static ICompressor GZip { get; }public static ICompressor Br { get; }private static void Register(string identifier, object component);public static void Register(ICompressor compresser);public static void Register(IEncoder encoder);public static bool TryGetComponentT(string id, out T component);public static string Zip(string str, ICompressor compressor, IEncoder encoder);public static string Zip(string str);public static string Unzip(string str);
}使用 StringZipper 压缩字符串最简单的使用方式就是直接调用 StringZipper 的 Zip 方法默认情况下会使用 Deflate 压缩算法和 Ascii85 编码using LuYao;
var input man is distinguished, not only by his reason, but also by this singular passion from other animals; in whom the appetite of food, and other pleasures of sense, by predominance, take away the care of knowing causes; which is a lust of the mind, that by a perseverance of delight in the continual and indefatigable generation of knowledge, exceedeth the short vehemence of any carnal pleasure.;
var output StringZipper.Zip(input);
Console.WriteLine(output);var bytesFrom Encoding.UTF8.GetByteCount(input);
var bytesTo Encoding.UTF8.GetByteCount(output);Console.WriteLine({0} {1} {2:0.##%},bytesFrom,bytesTo,1d*bytesTo/bytesFrom);输出data:text/x-deflate;ascii85,~23?4J%VHaG6SCZ)39YnDE7!KYd9D2tfhWJ,^cgm0hTEta?GJmU_ekKWRQilMfObfMl1a$n3T;ti;VBr]sfYusqtXm8:)4amP^.pgK[(QXW5PgSMh?,H9f94YdUQrJRc-tTg9*LGZ!ctH5dj6ZJZjlBPEZuWoYDp^A-WFo#t[io.[%/8EOoq4^IBD)XDC?UB_qFZ#KNe*9ZggakM-h-*mulYKa3gp.VOTjcfPKIRVtcTCa_NYjXPN,nMuui]b;FW)!NXquZ~
391 320 81.84%如果需要使用其他的压缩算法或编码方式Zip 方法有一个需要三个参数的重载将对应的静态属性传入即可。如果需要使用 Br 压缩和 Base64 编码则可以使用以下代码var output StringZipper.Zip(input, StringZipper.Br,StringZipper.Base62);使用 StringZipper 解压字符串与压缩时可以指定压缩和编码方式相比解压缩只有一个方法 Unzip 。该方法会自动判断传入的字符串是否被压缩以及压缩时使用的参数using LuYao;
var input data:text/x-deflate;ascii85,~23?4J%VHaG6SCZ)39YnDE7!KYd9D2tfhW\J,^cgm0hTEta?GJmU_ekKWRQilMfObfMl1a$n3T;ti;VBr]sfYusqtXm8:)4amP^.pgK[(QXW5PgSMh?,H9f94YdUQrJRc-tTg9*LGZ!ctH5dj6ZJZjlBPEZuWoYDp^A-WFo#t[io.[%/8EOoq4^IBD)XDC?UB_qFZ#KNe*9ZggakM-h-*mulYKa3gp.VOTjcfPKIRVtcTCa_NYjXPN,nMuui]b;FW)!NXquZ~;
var output StringZipper.Unzip(input);
Console.WriteLine(output);输出man is distinguished, not only by his reason, but also by this singular passion from other animals; in whom the appetite of food, and other pleasures of sense, by predominance, take away the care of knowing causes; which is a lust of the mind, that by a perseverance of delight in the continual and indefatigable generation of knowledge, exceedeth the short vehemence of any carnal pleasure.路遥工具箱中的功能集成路遥工具箱已经内置了【文本压缩】功能在【文字处理】菜单下。其采用的压缩、解压算法与本文所采用的相同。这就代表着可以在开发或者调试时很容易对已压缩的数据进行解压提高软件开发的效率。