Csharp/C#教程:C#实现压缩和解压缩的方法示例【Gzip和Zip方式】分享

本文实例讲述了C#实现压缩和解压缩的方法。分享给大家供大家参考,具体如下:

使用ICSharpCode.SharpZipLib.dll来压缩/解压(压缩效率比GZip要高一点)

publicstaticclassZipUtil { ///<summary> ///压缩 ///</summary> ///<paramname="param"></param> ///<returns></returns> publicstaticstringCompress(stringparam) { byte[]data=System.Text.Encoding.UTF8.GetBytes(param); //byte[]data=Convert.FromBase64String(param); MemoryStreamms=newMemoryStream(); Streamstream=newICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms); try { stream.Write(data,0,data.Length); } finally { stream.Close(); ms.Close(); } returnConvert.ToBase64String(ms.ToArray()); } ///<summary> ///解压 ///</summary> ///<paramname="param"></param> ///<returns></returns> publicstaticstringDecompress(stringparam) { stringcommonString=""; byte[]buffer=Convert.FromBase64String(param); MemoryStreamms=newMemoryStream(buffer); Streamsm=newICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms); //这里要指明要读入的格式,要不就有乱码 StreamReaderreader=newStreamReader(sm,System.Text.Encoding.UTF8); try { commonString=reader.ReadToEnd(); } finally { sm.Close(); ms.Close(); } returncommonString; } }

使用GZip来压缩/解压缩(字符串)

publicstaticclassGZipUtil { publicstaticstringZip(stringvalue) { //Transformstringintobyte[] byte[]byteArray=newbyte[value.Length]; intindexBA=0; foreach(chariteminvalue.ToCharArray()) { byteArray[indexBA++]=(byte)item; } //Prepareforcompress System.IO.MemoryStreamms=newSystem.IO.MemoryStream(); System.IO.Compression.GZipStreamsw=newSystem.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress); //Compress sw.Write(byteArray,0,byteArray.Length); //Close,DONOTFLUSHcausebyteswillgomissing... sw.Close(); //Transformbyte[]zipdatatostring byteArray=ms.ToArray(); System.Text.StringBuildersB=newSystem.Text.StringBuilder(byteArray.Length); foreach(byteiteminbyteArray) { sB.Append((char)item); } ms.Close(); sw.Dispose(); ms.Dispose(); returnsB.ToString(); } publicstaticstringUnZip(stringvalue) { //Transformstringintobyte[] byte[]byteArray=newbyte[value.Length]; intindexBA=0; foreach(chariteminvalue.ToCharArray()) { byteArray[indexBA++]=(byte)item; } //Preparefordecompress System.IO.MemoryStreamms=newSystem.IO.MemoryStream(byteArray); System.IO.Compression.GZipStreamsr=newSystem.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress); //Resetvariabletocollectuncompressedresult byteArray=newbyte[byteArray.Length]; //Decompress intrByte=sr.Read(byteArray,0,byteArray.Length); //Transformbyte[]unzipdatatostring System.Text.StringBuildersB=newSystem.Text.StringBuilder(rByte); //ReadthenumberofbytesGZipStreamredanddonotaforeachbytesin //resultByteArray; for(inti=0;i<rByte;i++) { sB.Append((char)byteArray[i]); } sr.Close(); ms.Close(); sr.Dispose(); ms.Dispose(); returnsB.ToString(); } }

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#常见控件用法教程》、《WinForm控件用法上述就是C#学习教程:C#实现压缩和解压缩的方法示例【Gzip和Zip方式】分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/907862.html

(0)
上一篇 2021年10月25日
下一篇 2021年10月25日

精彩推荐