C#利用MemoryStream压缩二进制内容(可以是String、图像等)

<p class="MsoNormal" style="text-indent: 21.2pt; margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">在网络上进行通信,压缩通信对于传输效率来讲非常重要,特别是要传输的内容比较复杂时。下面就以压缩</span><span lang="EN-US"><span style="font-family: Calibri;">String</span></span><span style="">对象为例,介绍一下压缩和解压任意对象的方法(无论什么对象都可以序列化为byte流):</span></span></p>
<p class="MsoNormal" style="text-indent: 21.2pt; margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="">先建立压缩类:</span></span></p>
<p><span style="font-family: NSimSun; color: blue; font-size: 9.5pt;" lang="EN-US">
</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; color: blue; font-size: 9.5pt;" lang="EN-US">using</span><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"> System;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; color: blue; font-size: 9.5pt;" lang="EN-US">using</span><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"> System.Collections.Generic;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; color: blue; font-size: 9.5pt;" lang="EN-US">using</span><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"> System.Linq;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; color: blue; font-size: 9.5pt;" lang="EN-US">using</span><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"> System.Text;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; color: blue; font-size: 9.5pt;" lang="EN-US">using</span><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"> System.IO;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; color: blue; font-size: 9.5pt;" lang="EN-US">using</span><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"> System.IO.Compression;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; color: blue; font-size: 9.5pt;" lang="EN-US">namespace</span><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"> Felomeng</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;">{</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">public</span><span style="color: #000000;"> </span><span style="color: blue;">class</span><span style="color: #000000;"> </span><span style="color: #2b91af;">MyZip</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>{</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">public</span><span style="color: #000000;"> </span><span style="color: blue;">static</span><span style="color: #000000;"> </span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;"> Compress(</span><span style="color: blue;">byte</span><span style="color: #000000;">[] inBytes)</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>{</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;"> outStream = </span><span style="color: blue;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;">();</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">using</span><span style="color: #000000;"> (</span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;"> intStream = </span><span style="color: blue;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;">(inBytes))</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>{</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">using</span><span style="color: #000000;"> (</span><span style="color: #2b91af;">GZipStream</span><span style="color: #000000;"> Compress =</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">GZipStream</span><span style="color: #000000;">(outStream,</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: #2b91af;">CompressionMode</span><span style="color: #000000;">.Compress))</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>{</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>intStream.CopyTo(Compress);</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>}</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>}</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">return</span><span style="color: #000000;"> outStream;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>}</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">public</span><span style="color: #000000;"> </span><span style="color: blue;">static</span><span style="color: #000000;"> </span><span style="color: blue;">byte</span><span style="color: #000000;">[] Decompress(</span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;"> inStream)</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>{</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">byte</span><span style="color: #000000;">[] result = </span><span style="color: blue;">null</span><span style="color: #000000;">;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;">compressedStream = </span><span style="color: blue;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;">(inStream.ToArray());</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">using</span><span style="color: #000000;"> (</span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;"> outStream = </span><span style="color: blue;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">MemoryStream</span><span style="color: #000000;">())</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>{</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">using</span><span style="color: #000000;"> (</span><span style="color: #2b91af;">GZipStream</span><span style="color: #000000;"> Decompress = </span><span style="color: blue;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">GZipStream</span><span style="color: #000000;">(compressedStream,</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: #2b91af;">CompressionMode</span><span style="color: #000000;">.Decompress))</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>{</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>Decompress.CopyTo(outStream);</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>result =outStream.ToArray();</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>}</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>}</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""><span style="color: #000000;"> </span></span><span style="color: blue;">return</span><span style="color: #000000;"> result;</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>}</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;"><span style=""> </span>}</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style="color: #000000;">}</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"></p>

<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="">用法如下所示:</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; color: #2b91af; font-size: 9.5pt;" lang="EN-US">String</span><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"> myString = <span style="color: #a31515;">"testCompress"</span>, <span style="color: green;">//</span></span><span style="">测试用</span><span style="font-family: NSimSun; color: green; font-size: 9.5pt;" lang="EN-US">string</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>result;<span style="color: green;">//</span></span><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"> </span></span><span style="">解压后结果</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: #2b91af;">MemoryStream</span> outFile = <span style="color: blue;">new</span> <span style="color: #2b91af;">MemoryStream</span>();<span style="color: green;">//outFile</span></span><span style="">就是压缩后存放的地方,用于网络传输的对象</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>outFile = Compress(<span style="color: #2b91af;">Encoding</span>.UTF8.GetBytes(myString));<span style="color: green;">//</span></span><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"> </span></span><span style="">压缩源字符串</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>result = <span style="color: #2b91af;">Encoding</span>.UTF8.GetString(Decompress(outFile));<span style="color: green;">//</span></span><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"> </span></span><span style="">解压得到源字符串</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"></span></p>
<p><span style="">如果想得到更好的压缩结果,可以使用比系统提供的</span><span style="" lang="EN-US">GZipStream</span><span style="">更好的压缩算法。</span></p>

你可能感兴趣的:(String)