C#字符串压缩&解压
来源:互联网 发布:php在线客服系统源码 编辑:程序博客网 时间:2024/06/05 16:34
/// <summary> /// 字符串压缩 /// </summary> /// <param name="strSource"></param> /// <returns></returns> public static byte[] Compress(byte[] data) { try { MemoryStream ms = new MemoryStream(); GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true); zip.Write(data, 0, data.Length); zip.Close(); byte[] buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); ms.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 字符串解压缩 /// </summary> /// <param name="strSource"></param> /// <returns></returns> public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true); MemoryStream msreader = new MemoryStream(); byte[] buffer = new byte[0x1000]; while (true) { int reader = zip.Read(buffer, 0, buffer.Length); if (reader <= 0) { break; } msreader.Write(buffer, 0, reader); } zip.Close(); ms.Close(); msreader.Position = 0; buffer = msreader.ToArray(); msreader.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } public static string CompressString(string str) { string compressString = ""; byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); byte[] compressAfterByte = Compress(compressBeforeByte); //compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); compressString = Convert.ToBase64String(compressAfterByte); return compressString; } public static string DecompressString(string str) { string compressString = ""; //byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); byte[] compressBeforeByte = Convert.FromBase64String(str); byte[] compressAfterByte = Decompress(compressBeforeByte); compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); return compressString; }
0 0
- C#字符串压缩&解压
- C#字符串压缩和解压
- C# 对字符串的压缩和解压
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- 字符串压缩和解压
- GZipStream压缩解压字符串
- C# 压缩与解压字符串(面试题)
- C# 压缩,解压
- c# 压缩解压文件
- C#压缩解压程序
- C# 压缩解压文件
- C# GZipStream 压缩 解压
- c# 压缩解压 方法
- c# 压缩解压文件
- c# 压缩解压代码
- C#压缩解压文件夹
- 轻应用能否创造社交营销新商业价值
- 单桂华-大规模天文时空数据的可视化
- as3与node.js的socket通信
- 【VC++】如何最大化对话框
- C++之动态绑定和静态绑定
- C#字符串压缩&解压
- WinCE注册表--创建、打开、读写、关闭
- Emacs 键绑定的格局
- Cocos2dx 3.0 过渡篇(八)浅谈容器Vector
- CXF+Spring+Tomcat简明示例
- 关于UITableViewCell的方法及属性
- js
- vi中查找和替换文字
- sqlserver数据库置疑解决