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 ...
随机推荐
- JavaScript 运用ES2015特性的小项目
阅读了<JavaScript Pattern>这本书,里面讲了很多js的本质概念以及项目的设计理念.很值得一看,这是我做的摘要,有兴趣的看官可以点这里.里面讲解mediator patte ...
- python感悟
嗯,对于python我其实也是一知半解,比较擅长java,我就拿java和python进行比较一下吧 1,首先先说简单的程序运行时间问题,对于同等时间复杂度的测试程序,python虽然运行速度没有c快 ...
- testng及JMeter使用之初体验
这里只是简单的说下我再项目中使用testng的JMeter使用的情况, 主要的是使用JMeter的体验, 都是以截图和文字为说明的. 1, 性能测试之testng 直接可以 ...
- CentOS7安装最新版git教程
下载编译工具 yum -y groupinstall "Development Tools" 下载依赖包 yum -y install zlib-devel perl-ExtUti ...
- 通向架构师的道路之 Tomcat 性能调优
一.总结前一天的学习 从"第三天"的性能测试一节中,我们得知了决定性能测试的几个重要指标,它们是: 吞吐量 Responsetime Cpuload MemoryUsage 我们也 ...
- Android_内部文件读取
我们这里做一个安卓的简易的文件读取程序之记住密码 首先我们先明确安卓的存储路径, 所有安装至手机的应用都会在 data/data 目录下生成一个安卓文件夹(包名),这个文件夹就是安卓存储的路径 在运行 ...
- Jquery给网页的title取值和赋值
//获取title的值 var title_val=$('#id').attr('title'); alert(title_val); //修改title的值.赋值给title $('#id2').a ...
- web服务器学习1---httpd-2.4.29源码手动编译安装
环境准备: 系统:CentOS 7.4 软件:httpd-2.4.29 一 Apache主要特点 apache服务器在功能,性能和安全性等方面表现比较突出,可以较好地满足web服务器地应用需求.主要 ...
- 【前端】wangEditor(富文本编辑器) 简易使用示例
转载请说明作者或者注明出处,谢谢 说到前端常用的编辑器,自然也少不了富文本编辑器(RichText Editor) 笔者在此之前也看了一些相关的在线编辑器,其中包括了当百度搜索“富文本编辑器”字样时出 ...
- Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 开源软件云收藏
云收藏项目已经开源2年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时很新的技术,现在看来其实很多新技术是没有必要使用的,但做为学习案例来讲确实是一个绝佳的 Spring ...