在.Net Compact Framework 3.5中使用GZip
2015-10-27 17:11
274 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/allen6lu/article/details/84746856
在.Net Compact Framework 3.5中将数据加压
using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.IO; using System.IO.Compression; namespace MRSDataSync.Helper { public class GZip { private Encoding encode = Encoding.UTF8; public byte[] zipData(byte[] inputData) { if (inputData == null) { throw new ArgumentNullException("input data cannot be null."); } byte[] result; #region MS compressor MemoryStream mso = null; try { mso = new MemoryStream(); GZipStream gzipStream = null; try { gzipStream = new GZipStream(mso, CompressionMode.Compress, false); gzipStream.Write(inputData, 0, inputData.Length); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace); throw; } finally { if (gzipStream != null) { gzipStream.Close(); gzipStream.Dispose(); gzipStream = null; } } result = mso.ToArray(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace); throw; } finally { if (mso != null) { mso.Close(); mso.Dispose(); mso = null; } } return result; #endregion } public byte[] unZipData(byte[] inputData) { if (inputData == null) { throw new ArgumentNullException("input data cannot be null."); } byte[] result; #region MS decompressor MemoryStream msi = null; MemoryStream mso = null; try { msi = new MemoryStream(inputData); mso = new MemoryStream(); GZipStream gzipStream = null; try { gzipStream = new GZipStream(msi, CompressionMode.Decompress, false); #region copy stream const int BUFFER_SIZE = 4096; byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; long totalBytesRead = 0; while ((bytesRead = gzipStream.Read(buffer, 0, BUFFER_SIZE)) != 0) { mso.Write(buffer, 0, bytesRead); totalBytesRead += bytesRead; } mso.Flush(); #endregion } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace); throw; } finally { if (gzipStream != null) { gzipStream.Close(); gzipStream.Dispose(); gzipStream = null; } } result = mso.ToArray(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace); throw; } finally { if (msi != null) { msi.Close(); msi.Dispose(); msi = null; } if (mso != null) { mso.Close(); mso.Dispose(); mso = null; } } return result; #endregion } public byte[] zipStr2Data(string inputData) { byte[] strData = encode.GetBytes(inputData); return zipData(strData); } public string unZipData2Str(byte[] inputdata) { byte[] outData = unZipData(inputdata); return encode.GetString(outData, 0, outData.Length); } } }
相关文章推荐
- Visual Studio 2008开发新特性系列课程(10):使用Visual Studio 2008和.NET Compact Framework 3.5创建Mobile应用程序
- Visual Studio 2008开发新特性系列课程(10):使用Visual Studio 2008和.NET Compact Framework 3.5创建Mobile应用程序
- .Net Compact Framework 泛型使用的注意点
- WCF for .Net Compact Framework 3.5 应用程序开发示例
- .NET Compact Framework 3.5 支持 WCF 的子集
- .Net Compact Framework 基础篇(11)--使用范型来创建控件
- 使用 Visual Studio 2005 为基于 Windows Mobile 的设备生成 .NET Compact Framework 应用程序
- 在 .NET Compact Framework 2.0 中使用序列化
- 使用 .NET Compact Framework 2.0 生成 Wi-Fi 发现应用程序
- WCF for .Net Compact Framework 3.5 应用程序开发示例
- 在没有安装.NET Framework 3.5的机器上使用LINQ等特性
- 使用 .NET Compact Framework 2.0 生成 Wi-Fi 发现应用程序 (转MSDN)
- 必须使用角色管理工具 安装或配置microsoft.net framework 3.5
- 解决【必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1】的方法
- 必须使用角色管理工具 安装或配置microsoft.net framework 3.5
- 密须使用“角色管理工具”安装或配置Microsoft .Net Framework 3.5。
- .Net Compact Framework 3.5对Linq的支持
- 在 .NET Compact Framework 2.0 中使用序列化
- .NET Compact Framework 下Win32 API P/Invoke 的使用
- 如何:在 .NET Compact Framework 中使用 DateTimePicker 类