具体可以了解下:http://msdn.microsoft.com/zh-cn/library/system.io.compression.deflatestream(v=vs.110).aspx

/// <summary>
/// Compresses data using the <see cref="DeflateStream"/> algorithm.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] DeflateData(byte[] data)
{
if (data == null) return null; byte[] buffer = null;
using (MemoryStream stream = new MemoryStream())
{
using (DeflateStream inflateStream = new DeflateStream(stream, CompressionMode.Compress, true))
{
inflateStream.Write(data, , data.Length);
} stream.Seek(, SeekOrigin.Begin); int length = Convert.ToInt32(stream.Length);
buffer = new byte[length];
stream.Read(buffer, , length);
} return buffer;
} /// <summary>
/// Inflates compressed data using the <see cref="DeflateStream"/> algorithm.
/// </summary>
/// <param name="compressedData"></param>
/// <returns></returns>
public static byte[] InflateData(byte[] compressedData)
{
if (compressedData == null) return null; // initialize the default lenght to the compressed data length times 2
int deflen = compressedData.Length * ;
byte[] buffer = null; using (MemoryStream stream = new MemoryStream(compressedData))
{
using (DeflateStream inflatestream = new DeflateStream(stream, CompressionMode.Decompress))
{
using (MemoryStream uncompressedstream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(uncompressedstream))
{
int offset = ;
while (true)
{
byte[] tempbuffer = new byte[deflen]; int bytesread = inflatestream.Read(tempbuffer, offset, deflen); writer.Write(tempbuffer, , bytesread); if (bytesread < deflen || bytesread == ) break;
} // end while uncompressedstream.Seek(, SeekOrigin.Begin);
buffer = uncompressedstream.ToArray();
}
}
}
} return buffer;
}

使用DeflateStream压缩与解压的更多相关文章

  1. HttpClient与APS.NET Web API:请求内容的压缩与解压

    首先说明一下,这里的压缩与解压不是通常所说的http compression——那是响应内容在服务端压缩.在客户端解压,而这里是请求内容在客户端压缩.在服务端解压. 对于响应内容的压缩,一般Web服务 ...

  2. 自定义DelegatingHandler为ASP.NET Web Api添加压缩与解压的功能

    HTTP协议中的压缩 Http协议中使用Accept-Encoding和Content-Encoding头来表示期望Response内容的编码和当前Request的内容编码.而Http内容的压缩其实是 ...

  3. 浅谈在c#中使用Zlib压缩与解压的方法

    作者:Compasslg 介绍 近期用c#开发一个游戏的存档编辑工具需要用 Zlib 标准的 Deflate 算法对数据进行解压. 在 StackOverflow 上逛了一圈,发现 c# 比较常用到的 ...

  4. Linux操作系统中,*.zip、*.tar、*.tar.gz、*.tar.bz2、*.tar.xz、*.jar、*.7z等格式的压缩与解压

    zip格式 压缩: zip -r [目标文件名].zip [原文件/目录名] 解压: unzip [原文件名].zip 注:-r参数代表递归 tar格式(该格式仅仅打包,不压缩) 打包:tar -cv ...

  5. Linux压缩与解压常用命令

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  6. Asp.net中文件的压缩与解压

    这里笔者为大家介绍在asp.net中使用文件的压缩与解压.在asp.net中使用压缩给大家带来的好处是显而易见的,首先是减小了服务器端文件存储的空间,其次下载时候下载的是压缩文件想必也会有效果吧,特别 ...

  7. Java实现文件压缩与解压

    Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例.(转载自http://www.puiedu. ...

  8. Zip 压缩、解压技术在 HTML5 浏览器中的应用

    JSZip 是一款可以创建.读取.修改 .zip 文件的 javaScript 工具.在 web 应用中,免不了需要从 web 服务器中获取资源,如果可以将所有的资源都合并到一个 .zip 文件中,这 ...

  9. linux下压缩与解压(zip、unzip、tar)详解

    linux下压缩与解压(zip.unzip.tar)详解 2012-05-09 13:58:39| 分类: linux | 标签:linux zip unzip tar linux命令详解 |举报|字 ...

随机推荐

  1. SpringBoot ControllerAdvice

    在Spring3.2中新增了@ControllerAdvice注解,可用于定义@ExceptionHandler @ModelAttribute @InitBinder,并应用到所有被@Request ...

  2. centos 6的LAMP一键安装包(可选择/升级版本)

    安装步骤 事前准备(安装 wget.screen.unzip,创建 screen 会话) yum -y install wget screen git git clone 并赋予脚本执行权限 git ...

  3. spring security采用基于持久化 token 的方法实现的remember me功能

    采用该方法相较于简单加密方式安全一些.具体的原理见 http://wiki.jikexueyuan.com/project/spring-security/remember-me.html  一.建立 ...

  4. Mac下编译tesseract报错 DotProductAVX can't be used on Android

    因为我的mac是64位的,所以用32位编译,执行的时候肯定会出错的. 所以应该在 arch/simddetect.cpp中把这句# define X86_BUILD 1 注释掉,就可以了. 参考 ht ...

  5. TED字幕摘抄

    1.丹·吉尔伯特: 我们为什么快乐?http://v.163.com/movie/2012/12/0/S/M8HHB6LDT_M8HHCBM0S.html 在两百万年中, 大脑脑容量从我们祖先能人的1 ...

  6. linux service start|stop|restart

    用了这么些日子的linux/unix系统,也和别人一起合作开发了不少程序,发现高手都喜欢在命令行上操作,而且控制程序的运行偏好于使用脚本,加上参数如:start.restart.stop等. 后来自己 ...

  7. ZC_操作_not敲代码

    1.javah 命令(路径为 项目的bin目录下),例如 : F:\ZC_Code_E\workspace__MyEclipse2013\JNIjw01\bin>javah jniZ.JNIjw ...

  8. DOM元素的位置、尺寸及更多的信息

    一.基本概念 document.documentElement是整个DOM树的根节点,对应的元素就是html.下面将其称作根元素或根节点. document.body,对应的元素是body 二.浏览器 ...

  9. C# 集合类 Array,Arraylist,List,Hashtable,Dictionary...

    我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类.我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和 ...

  10. lightoj1370欧拉函数/素数筛

    这题有两种解法,1是根据欧拉函数性质:素数的欧拉函数值=素数-1(可根据欧拉定义看出)欧拉函数定义:小于x且与x互质的数的个数 #include<map> #include<set& ...