当前位置: 编程技术>.net/c#/asp.net
c#压缩数据传输的代码一例
来源: 互联网 发布时间:2014-08-30
本文导语: 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.IO; using ICSharpCode.SharpZipLib.BZip2; using System.Text; namespace Common { /// /// 压缩数据传输 /// http://www. /// public class UnZip { public UnZip() ...
代码如下:
using System; using System.Data; using System.Configuration; using System.Web; using System.IO; using ICSharpCode.SharpZipLib.BZip2; using System.Text; namespace Common { /// /// 压缩数据传输 /// http://www. /// public class UnZip { public UnZip() { } #region 返回压缩后的字节数组 /// /// 返回压缩后的字节数组 /// /// /// public static byte[] Compress(byte[] data) { if (data == null || data.Length == 0) { return null; } MemoryStream inStream = new MemoryStream(data, 0, data.Length); MemoryStream outStream = new MemoryStream(); BZip2.Compress(inStream, outStream,false, data.Length); byte[] result = outStream.ToArray(); inStream.Close(); outStream.Close(); return result; } #endregion #region 返回解压后的字节数组 /// /// 返回解压后的字节数组 /// /// 原始字节数组 /// public static byte[] Decompress(byte[] data) { if (data == null || data.Length == 0) { return null; } MemoryStream inStream = new MemoryStream(data, 0, data.Length); MemoryStream outStream = new MemoryStream(); BZip2.Decompress(inStream, outStream,false); byte[] result = outStream.ToArray(); inStream.Close(); outStream.Close(); return result; } #endregion } }