博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# GZip对字符串压缩和解压
阅读量:4446 次
发布时间:2019-06-07

本文共 3355 字,大约阅读时间需要 11 分钟。

C# GZip对字符串压缩和解压

using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.IO;  using System.IO.Compression;  using System.Data;    namespace HelperProjects  {      public class ZipHelper      {          ///           /// 解压          ///           ///           /// 
public static DataSet GetDatasetByString(string Value) { DataSet ds = new DataSet(); string CC = GZipDecompressString(Value); System.IO.StringReader Sr = new StringReader(CC); ds.ReadXml(Sr); return ds; } /// /// 根据DATASET压缩字符串 /// /// ///
public static string GetStringByDataset(string ds) { return GZipCompressString(ds); } /// /// 将传入字符串以GZip算法压缩后,返回Base64编码字符 /// /// 需要压缩的字符串 ///
压缩后的Base64编码的字符串
public static string GZipCompressString(string rawString) { if (string.IsNullOrEmpty(rawString) || rawString.Length == 0) { return ""; } else { byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString()); byte[] zippedData = Compress(rawData); return (string)(Convert.ToBase64String(zippedData)); } } /// /// GZip压缩 /// /// ///
public static byte[] Compress(byte[] rawData) { MemoryStream ms = new MemoryStream(); GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true); compressedzipStream.Write(rawData, 0, rawData.Length); compressedzipStream.Close(); return ms.ToArray(); } /// /// 将传入的二进制字符串资料以GZip算法解压缩 /// /// 经GZip压缩后的二进制字符串 ///
原始未压缩字符串
public static string GZipDecompressString(string zippedString) { if (string.IsNullOrEmpty(zippedString) || zippedString.Length == 0) { return ""; } else { byte[] zippedData = Convert.FromBase64String(zippedString.ToString()); return (string)(System.Text.Encoding.UTF8.GetString(Decompress(zippedData))); } } /// /// ZIP解压 /// /// ///
public static byte[] Decompress(byte[] zippedData) { MemoryStream ms = new MemoryStream(zippedData); GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress); MemoryStream outBuffer = new MemoryStream(); byte[] block = new byte[1024]; while (true) { int bytesRead = compressedzipStream.Read(block, 0, block.Length); if (bytesRead <= 0) break; else outBuffer.Write(block, 0, bytesRead); } compressedzipStream.Close(); return outBuffer.ToArray(); } } }  

转载于:https://www.cnblogs.com/yangyangblog/p/9282111.html

你可能感兴趣的文章
inux中bin与sbin目录的作用及区别介绍
查看>>
USACO 3.1 Contact
查看>>
Office之什么是高内聚低耦合
查看>>
一些奇怪的问题求回答
查看>>
这些年踩过的坑
查看>>
iOS开发拓展篇——如何把项目托管到GitHub
查看>>
性能优化之数据库优化
查看>>
类的继承、菱形继承、派生、多态
查看>>
mysql约束
查看>>
javascript鼠标及键盘事件总结及案例
查看>>
mysql表之间的关系及级联操作
查看>>
mac 搭建virtualenv的那些坑
查看>>
多路复用IO模型
查看>>
并发、串行、并行及多道技术原理
查看>>
hashlib、pickle、hmac、logging模块使用
查看>>
javascript常用知识点总结
查看>>
2019秋招复习笔记--数据库基本操作
查看>>
2019秋招复习笔试--手写代码
查看>>
2019秋招复习笔记--智力题
查看>>
MySQL学习笔记
查看>>