GZip 压缩及解压缩
/// <summary>
/// GZipHelper
/// </summary>
public class GZipHelper
{
/// <summary>
/// 将传入字符串以GZip算法压缩后,返回Base64编码字符
/// </summary>
/// <param name="rawString">需要压缩的字符串</param>
/// <returns>
/// 压缩后的Base64编码的字符串
/// </returns>
public static string GZipCompressString(string rawString)
{
if (string.IsNullOrEmpty(rawString) || rawString.Length == )
return "";
return Convert.ToBase64String(GZipHelper.Compress(Encoding.UTF8.GetBytes(rawString.ToString())));
} /// <summary>
/// GZip压缩
/// </summary>
/// <param name="rawData"/>
/// <returns/>
private static byte[] Compress(byte[] rawData)
{
MemoryStream memoryStream = new MemoryStream();
int num1 = ;
int num2 = ;
GZipStream gzipStream = new GZipStream((Stream) memoryStream, (CompressionMode) num1, num2 != );
byte[] buffer = rawData;
int offset = ;
int length = rawData.Length;
gzipStream.Write(buffer, offset, length);
gzipStream.Close();
return memoryStream.ToArray();
} /// <summary>
/// 将传入的二进制字符串资料以GZip算法解压缩
/// </summary>
/// <param name="zippedString">经GZip压缩后的二进制字符串</param>
/// <returns>
/// 原始未压缩字符串
/// </returns>
public static string GZipDecompressString(string zippedString)
{
if (string.IsNullOrEmpty(zippedString) || zippedString.Length == )
return "";
return Encoding.UTF8.GetString(GZipHelper.Decompress(Convert.FromBase64String(zippedString.ToString())));
} /// <summary>
/// GZIP解压
/// </summary>
/// <param name="zippedData"/>
/// <returns/>
public static byte[] Decompress(byte[] zippedData)
{
GZipStream gzipStream = new GZipStream((Stream) new MemoryStream(zippedData), CompressionMode.Decompress);
MemoryStream memoryStream = new MemoryStream();
byte[] buffer = new byte[];
while (true)
{
int count = gzipStream.Read(buffer, , buffer.Length);
if (count > )
memoryStream.Write(buffer, , count);
else
break;
}
gzipStream.Close();
return memoryStream.ToArray();
}
}
GZip 压缩及解压缩的更多相关文章
- GZIP压缩、解压缩工具类
GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...
- 对数据进行GZIP压缩或解压缩
/** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...
- GZip压缩与解压缩
GZIP的压缩与解压缩代码: public static class CompressionHelper { /// <summary> /// Compress the byte[] / ...
- java GZIP压缩与解压缩
1.GZIP压缩 public static byte[] compress(String str, String encoding) { if (str == null || str.length( ...
- 压缩与解压缩 gzip bzip2 tar 命令
gzip压缩与解压缩 命令 gzip -v 解压缩 gzip-d 操作如下. 压缩 .可以看到源文件有5171大小,压缩后,变成了1998大小. 解压缩 .解压缩之后可以看到,原来的man_db ...
- gzip [选项] 压缩(解压缩)
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...
- Linux命令(十八) 压缩或解压缩文件和目录 gzip gunzip
目录 1.命令简介 2.常用参数介绍 3.实例 4.直达底部 命令简介 和 zip 命令类似,gzip 用于文件的压缩,gzip压缩后的文件扩展名为 ".gz",gzip默认压缩后 ...
- c#实现gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
转载:https://blog.csdn.net/luanpeng825485697/article/details/78165788 我测试了下压缩byte[],是可以的 using System; ...
- Python3 压缩与解压缩(zlib / gzip / bz2 / lzma / zipfile / tarfile)
本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/72672703 以下代码以Python3.6.1为例 L ...
随机推荐
- Android 音视频编解码——RGB与YUV格式转换
一.RGB模型与YUV模型 1.RGB模型 我们知道物理三基色分别是红(Red).绿(Green).蓝(Blue).现代的显示器技术就是通过组合不同强度的红绿蓝三原色,来达成几乎任何一种可见光的颜色. ...
- 大数据(3):基于sogou.500w.utf8数据Hbase和Spark实践
1. HBase安装部署操作 a) 解压HBase安装包tar –zxvf hbase-0.98.0-hadoop2-bin.tar.gzb) 修改环境变量 hbase-env.shexport JA ...
- 关于Sql server数据 MD5加密
最近在写一个web项目时,需要在数据库中将用户密码等一类信息进行加密处理.数据加密算法有许多 ,各有各的优缺点;在 http://www.cnblogs.com/yangywyangyw/arch ...
- 简单http代理服务器搭建
1. yum install squid2. vi /etc/squid/squid.conf 将http_access deny all 中deny 改为allow,http_port后面的是端口号 ...
- PPT分享 | 怎么在区块链上保护隐私?
艾伯特AI人工智能(公众号:aibbtcom)按: (编者按:在12月3日的亚太区以太坊社区培训和交流Meetup深圳站上,Vitalik作了<怎么在区块链上保护隐私>的演讲,谈到了区块链 ...
- 检查硬件变化的命令kudzu
当新加一个硬件时,系统并没有出现,可以通过这个命令来检查下:
- unable to apply changes:plugins "App links assistant",firebase services won'
新安装的android studio工具报错,本来以为只要在plugins中安装android support插件的,现在点击该插件还是不能apply,提示: unable to apply chan ...
- IPFS家族(二)
go-ipfs IPFS协议的go语言实现,ipfs的核心协议,最新版是v0.4.13 下载地址:https://dist.ipfs.io/#go-ipfs 源代码地址:https://github. ...
- 笔记:Maven 生成项目站点
Maven 不仅仅时一个自动化构建工具和一个依赖管理工具,他还能够帮助聚合项目信息,促进团队间的交流,POM 可以包含各种项目信息,如项目描述.版本控制系统地址.缺陷跟踪系统地址.许可证信息.开发者信 ...
- 【Django】 初步学习
这个系列(或者成不了一个系列..)预计会全程参考Vamei様的Django系列,膜一发.说句题外话,其实更加崇拜像Vamei那样的能够玩转生活.各个领域都能取得不小成就的人. [Django] ■ 概 ...