- 浏览: 904743 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
yaawaas:
有了 。。
程序员装B指南 -
niepeng880208:
藐视我还是初级装
程序员装B指南 -
sa364867195:
哥们,现在实现了吗?
GPS卫星定位车辆监控系统 -
月下小人:
顶,这才是质的提高,果断顶
一个基于jQuery ajax和.net httphandler 的超轻异步框架,千行代码完成。 -
xp9802:
有意思,动静结合
程序员装B指南
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>
<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>
相关推荐
在C#编程中,`MemoryStream`类是一个非常重要的工具,尤其在处理二进制数据与字符编码转换时。`MemoryStream`是.NET框架中System.IO命名空间的一部分,它允许我们在内存中创建一个流,而无需写入或读取文件。在本...
在C#中,可以使用MemoryStream和Image.FromStream将二进制数组转换成图片。具体实现代码如下: ```csharp public void ShowImgByByte(byte[] imgBytesIn) { MemoryStream ms = new MemoryStream(imgBytesIn); ...
在C#中,我们可以使用`System.IO`命名空间中的`FileStream`、`MemoryStream`和`BinaryReader`、`BinaryWriter`类来读取和写入二进制流。 1. 图片转换为二进制流: 要将图片转换为二进制流,首先需要使用`System....
在C#中,图片本质上是一个二进制数据流,可以通过文件流(FileStream)读取图片文件并将其转换为字节数组(byte[])。`GetPictureData`函数就是一个这样的例子,它接受一个图片路径作为参数,然后创建一个FileStream对象...
在C#中,可以使用MemoryStream类来将二进制流转换成图片。下面是一个简单的示例代码: ```csharp using System.Drawing; // 将二进制流转换成图片 MemoryStream ms = new MemoryStream(fileByte); Image img = ...
在C#编程中,文件和二进制流的转换是一个常见的操作,特别是在处理图像、音频、视频等非文本数据时。二进制流是计算机内存中以0和1表示的数据流,它可以精确地保存和传输文件的所有原始信息。本文将深入探讨如何在C#...
在C#编程环境中,处理二进制数据,特别是图片,是一项常见的任务。本文将深入探讨如何在C#中进行二进制图片的读写操作,同时结合Visual Studio 2005(VS2005)和SQL Server数据库进行存储和检索。了解这些技巧对于...
二进制文件可以包含各种类型的数据,如图像、音频、视频、系统配置或自定义数据结构。本项目聚焦于使用C#语言来解析这些文件,特别是针对ASCII和16进制字符串等格式。以下是对这个" C# 解析二进制文件程序 "的详细...
在C#编程中,二进制读写操作是处理数据流的一种重要技术,尤其是在需要存储或传输原始字节数据时。二进制读写器(BinaryReader和BinaryWriter)是.NET框架提供的一对类,用于从流中读取和向流中写入基本类型的数据。...
在C#编程环境中,将图片转化为二进制数据是一种常见的操作,这有助于在数据库存储、网络传输或文件处理等场景中高效地处理图像资源。在.NET框架下,C#提供了丰富的类库支持这类操作。本篇文章将深入探讨如何将图片...
二进制文件通常用于存储原始数据,如图像、音频、视频或自定义的数据结构,因为它们可以高效地保存和读取大量信息。以下是对这个主题的详细阐述: 一、二进制数据的理解 二进制数据是计算机理解和处理数据的基本...
总结来说,"C#二进制图片测试.zip"中的内容可能涉及了C#编程中处理二进制图片的基本步骤,包括读取二进制文件、将二进制数据解析为图像、图像的二进制写入以及可能的Base64转换。这些都是开发过程中处理图像数据时必...
十六进制表示法在处理二进制数据时非常有用,因为它使得数据更易读且便于在文本格式中传输。在C#中,可以使用BitConverter类将字节数组转换为十六进制字符串,反之亦然。 总结来说,C#实现的十六进制AES加解密结合...
在C#中,System.IO命名空间提供了一系列类来处理二进制流,如FileStream、MemoryStream和NetworkStream等。 **文件上传**: 在C#中,文件上传通常涉及到HTTP协议,特别是POST请求。当用户选择一个文件并提交表单时...
二进制数据可以还原为BMP图像,只需将字节数组`bufPic`传入`MemoryStream`,然后使用`Image.FromStream`创建`Image`对象,再将其赋值给`PictureBox`或其他显示组件: ```csharp using (MemoryStream ms = new ...
- 数据验证:在输入数据到达数据库之前进行验证,可以使用`String.Length`属性检查字符串长度,对于二进制数据,可以使用`MemoryStream`或`BinaryReader`来计算其大小。 - 使用数据库提供的函数:例如,SQL Server...
C#中的System.IO命名空间提供了与文件和二进制数据交互的类,如FileStream和BinaryReader/Writer,可以用来读取和写入二进制数据。 存取过程通常包括以下步骤: 1. **读取图片**: 使用`System.Drawing.Image`类从...
在本场景中,它包含了压缩的图像二进制数据。C#通过使用`System.IO`命名空间下的类,如`FileStream`和`BinaryReader`,可以读取和解析这种文件的内容。读取DAT文件时,需要知道文件内部的结构和编码方式,以便正确地...
在C#中,可以使用.NET框架提供的System.Runtime.Serialization.Formatters.Binary命名空间中的BinaryFormatter类实现二进制序列化。 要对一个对象进行二进制序列化,首先需要使其可序列化。这可以通过在类上添加...
二进制大型对象(Binary Large Object,简称BLOB)是用来存储非结构化数据如图像、文档等的字段类型。在SQL Server 2005中,我们通常使用`VARBINARY(MAX)`或`IMAGE`数据类型来存储图片。`VARBINARY(MAX)`是SQL ...