C#之数据流和字符串压缩
来源:互联网 发布:樱井知香番号封面 编辑:程序博客网 时间:2024/05/29 08:09
.Net自带的类库,System.IO.Compress中自带GZip压缩。
using System;using System.IO;using System.IO.Compression;using System.Text;public class GZipUtil {/// <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); } } /// <summary>/// 字符串压缩/// </summary>/// <returns>The string.</returns>/// <param name="str">String.</param>public static string CompressString(string str) { string compressString = ""; byte[] compressBeforeByte = Encoding.UTF8.GetBytes(str); byte[] compressAfterByte = Compress(compressBeforeByte); //compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); compressString = Convert.ToBase64String(compressAfterByte); return compressString; } /// <summary>/// 字符串解压缩/// </summary>/// <returns>The string.</returns>/// <param name="str">String.</param>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.UTF8.GetString(compressAfterByte); return compressString; } }
阅读全文
0 0
- C#之数据流和字符串压缩
- c#中关于压缩数据流和解压数据流的实现
- C#字符串压缩和解压
- c#实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
- C# 对字符串的压缩和解压
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- Boost::iostreams 库编译和压缩数据流
- C#字符串压缩&解压
- Kettle数据流中空字符串和NULL值
- Java 压缩数据流Java 压缩数据流
- c#字符串压缩与解压缩
- C#数据流
- C#数据流
- 字符串压缩和解压
- 字符串翻转和字符串压缩
- 读书笔记: Java用zip和gzip压缩数据流
- 五分钟学会Android热修复
- 南阳理工acm 91阶乘之和
- eclipse启动Tomcat服务输入http://localhost:8080/报404解决方法
- JAVA---多线程
- 使用Jsoup简单解析HTML文件示例
- C#之数据流和字符串压缩
- 理解objc_msgSend的作用
- 使用javac命令行编译一个目录下的所有.java文件?
- linux sort
- 深入了解DHCP的4步租约过程
- android studio实现高德地图api定位
- php正则表达式(基本概念整合)
- android 未root手机获取应用的sqlite数据库
- 【Android笔记】一些常用的代码