/// <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 压缩及解压缩的更多相关文章

  1. GZIP压缩、解压缩工具类

    GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...

  2. 对数据进行GZIP压缩或解压缩

    /** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...

  3. GZip压缩与解压缩

    GZIP的压缩与解压缩代码: public static class CompressionHelper { /// <summary> /// Compress the byte[] / ...

  4. java GZIP压缩与解压缩

    1.GZIP压缩 public static byte[] compress(String str, String encoding) { if (str == null || str.length( ...

  5. 压缩与解压缩 gzip bzip2 tar 命令

    gzip压缩与解压缩 命令  gzip -v   解压缩 gzip-d 操作如下. 压缩 .可以看到源文件有5171大小,压缩后,变成了1998大小. 解压缩 .解压缩之后可以看到,原来的man_db ...

  6. gzip [选项] 压缩(解压缩)

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...

  7. Linux命令(十八) 压缩或解压缩文件和目录 gzip gunzip

    目录 1.命令简介 2.常用参数介绍 3.实例 4.直达底部 命令简介 和 zip 命令类似,gzip 用于文件的压缩,gzip压缩后的文件扩展名为 ".gz",gzip默认压缩后 ...

  8. c#实现gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩

    转载:https://blog.csdn.net/luanpeng825485697/article/details/78165788 我测试了下压缩byte[],是可以的 using System; ...

  9. Python3 压缩与解压缩(zlib / gzip / bz2 / lzma / zipfile / tarfile)

    本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/72672703 以下代码以Python3.6.1为例 L ...

随机推荐

  1. Xampp相关命令

    http://www.upwqy.com/details/5.html 1 启动关闭 需要切换目录: # cd /opt/lampp/ 启动 XAMPP # ./lampp start 停止 XAMP ...

  2. 用js实现左右阴影的切换

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  3. MSIL实用指南-创建枚举类型

    创建枚举类型比较简单,主要使用moduleBuilder.DefineEnum 和enumBuilder.DefineLiteral. 第一步:创建 EnumBuilder 创建 EnumBuilde ...

  4. [Oracle]UNIX与Windows 2000上Oracle的差异(I)

    作者:Ian Adam & David Stien, SAIC Ltd 日期:19-Dec-2003 出处:http://www.dbanotes.net翻译:Fenng ---------- ...

  5. 【Python】正则表达式re

    re 正则表达式(regular expression)这玩意儿多nb就不用说了,python用re模块来支持正则 首先是一些正则表达式的概念 1. 通配符 . 2. 多字符选择 [...] [abc ...

  6. 【CSS】 CSS基础知识 属性和选择

    css基础知识 html的基本标签都是千篇一律的,为了能够个性化外观,就需要进行样式的调整,而css就是专门用来维护,管理样式的一种格式.在html中定义css有三种方法 1. 为标签添加style属 ...

  7. 设计模式 --> MVC,MVP 和 MVVM 的图示

    MVC,MVP 和 MVVM 的图示 复杂的软件必须有清晰合理的架构,否则无法开发和维护.MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用. 一.MVC M ...

  8. Python类中的self到底是干啥的

    Python类中的self到底是干啥的 Python编写类的时候,每个函数参数第一个参数都是self,一开始我不管它到底是干嘛的,只知道必须要写上.后来对Python渐渐熟悉了一点,再回头看self的 ...

  9. 如何打包静态库.a文件 iOS

    代码调试好了开始打包成sdk,下面是将要打包的FRSDK代码(FRSDK.h暴露在外面有别人调用) 1.创建新工程(Xcode File-New-Project) 2.把下面的红色框的东西移除 3.将 ...

  10. 用js来实现那些数据结构(数组篇01)

    在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ...