.net 利用 GZipStream 压缩和解压缩
1.GZipStream 类
此类在 .NET Framework 2.0 版中是新增的。
提供用于压缩和解压缩流的方法和属性
2.压缩byte[]
- /// <summary>
- /// 压缩数据
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public byte[] Compress(byte[] data)
- {
- MemoryStream ms = new MemoryStream();
- GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress);
- zipStream.Write(data, 0, data.Length);//将数据压缩并写到基础流中
- zipStream.Close();
- return ms.ToArray();
- }
3.解压byte[]
- /// 解压数据
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public byte[] Decompress(byte[] data)
- {
- MemoryStream srcMs = new MemoryStream(data);
- GZipStream zipStream = new GZipStream(srcMs, CompressionMode.Decompress);
- MemoryStream ms = new MemoryStream();
- byte[] bytes = new byte[40960];
- int n;
- while ((n = zipStream.Read(bytes, 0, bytes.Length)) > 0)
- {
- ms.Write(bytes, 0, n);
- }
- zipStream.Close();
- return ms.ToArray();
- }
4.压缩byte[]数据,存放到文件中
- /// <summary>
- /// 将指定的字节数组压缩,并写入到目标文件
- /// </summary>
- /// <param name="srcBuffer">指定的源字节数组</param>
- /// <param name="destFile">指定的目标文件</param>
- public static void CompressData(byte[] srcBuffer, string destFile)
- {
- FileStream destStream = null;
- GZipStream compressedStream = null;
- try
- {
- //打开文件流
- destStream = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write);
- //指定压缩的目的流(这里是文件流)
- compressedStream = new GZipStream(destStream, CompressionMode.Compress, true);
- //往目的流中写数据,而流将数据写到指定的文件
- compressedStream.Write(srcBuffer, 0, srcBuffer.Length);
- }
- catch (Exception ex)
- {
- throw new Exception(String.Format("压缩数据写入文件{0}时发生错误", destFile), ex);
- }
- finally
- {
- // Make sure we allways close all streams
- if (null != compressedStream)
- {
- compressedStream.Close();
- compressedStream.Dispose();
- }
- if (null != destStream)
- destStream.Close();
- }
- }
5.解压文件,得到byte[]数据
- /// <summary>
- /// 将指定的文件解压,返回解压后的数据
- /// </summary>
- /// <param name="srcFile">指定的源文件</param>
- /// <returns>解压后得到的数据</returns>
- public static byte[] DecompressData(string srcFile)
- {
- if (false == File.Exists(srcFile))
- throw new FileNotFoundException(String.Format("找不到指定的文件{0}", srcFile));
- FileStream sourceStream = null;
- GZipStream decompressedStream = null;
- byte[] quartetBuffer = null;
- try
- {
- sourceStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
- decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);
- // Read the footer to determine the length of the destiantion file
- //GZIP文件格式说明:
- //10字节的头,包含幻数、版本号以及时间戳
- //可选的扩展头,如原文件名
- //文件体,包括DEFLATE压缩的数据
- //8字节的尾注,包括CRC-32校验和以及未压缩的原始数据长度(4字节) 文件大小不超过4G
- //为Data指定byte的长度,故意开大byte数据的范围
- //读取未压缩的原始数据长度
- quartetBuffer = new byte[4];
- long position = sourceStream.Length - 4;
- sourceStream.Position = position;
- sourceStream.Read(quartetBuffer, 0, 4);
- int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
- byte[] data;
- if (checkLength <= sourceStream.Length)
- {
- data = new byte[Int16.MaxValue];
- }
- else
- {
- data = new byte[checkLength + 100];
- }
- //每100byte从解压流中读出数据,并将读出的数据Copy到Data byte[]中,这样就完成了对数据的解压
- byte[] buffer = new byte[100];
- sourceStream.Position = 0;
- int offset = 0;
- int total = 0;
- while (true)
- {
- int bytesRead = decompressedStream.Read(buffer, 0, 100);
- if (bytesRead == 0)
- break;
- buffer.CopyTo(data, offset);
- offset += bytesRead;
- total += bytesRead;
- }
- //剔除多余的byte
- byte[] actualdata = new byte[total];
- for (int i = 0; i < total; i++)
- actualdata[i] = data[i];
- return actualdata;
- }
- catch (Exception ex)
- {
- throw new Exception(String.Format("从文件{0}解压数据时发生错误", srcFile), ex);
- }
- finally
- {
- if (sourceStream != null)
- sourceStream.Close();
- if (decompressedStream != null)
- decompressedStream.Close();
- }
- }
6.小结
压缩,解压都用GZipStream,操作的对象时普通流MemoryStream,不同的是:
压缩是将btye[]型的数据写入GZipStream中,而解压的时候是将GzipStream中的数据写入到byte[]中,并将读出的数据写入到MemoryStream后一次性输出
压缩到文件与压缩成byte[]不同的是压缩到文件利用到了FileStream将流写到文件,解压Gzip文件,需要根据文件的规则进行:后4位记录未压缩前的长度,根据该长度可以将解压出来的文件存放到稍大的byte[]中
- GZipUtil.rar (1.8 KB)
.net 利用 GZipStream 压缩和解压缩的更多相关文章
- 利用ICSharpCode进行压缩和解压缩
说说我利用ICSharpCode进行压缩和解压缩的一些自己的一下实践过程 1:组件下载地址 参考文章:C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件 2: 文件类 // ...
- C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件
我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...
- 在C#中利用SharpZipLib进行文件的压缩和解压缩收藏
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net(http://www.icsharpcode.net/OpenSource/SharpZipL ...
- 利用SharpZipLib进行字符串的压缩和解压缩
http://www.izhangheng.com/sharpziplib-string-compression-decompression/ 今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用 ...
- C#利用SharpZipLib进行文件的压缩和解压缩
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...
- C#- 压缩和解压缩的研究 .
用了第二种方法,感觉很不错,其他都没用过了.摘录下来,做一个备忘. 最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip ...
- .net中压缩和解压缩的处理
最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip 参考代码如下: //======================= ...
- 关于webservice大数据量传输时的压缩和解压缩
当访问WebSerivice时,如果数据量很大,传输数据时就会很慢.为了提高速度,我们就会想到对数据进行压缩.首先我们来分析一下. 当在webserice中传输数据时,一般都采用Dataset进行数据 ...
- iOS中使用ZipArchive压缩和解压缩文件-备
为什么我需要解压缩文件 有许多原因能解释为什么我要在工程中使用压缩和解压缩功能,下面是几个常见的原因: 苹果App Store的50M下载限制 苹 果公司出于流量的考虑,规定在非WIFI环境下,限制用 ...
随机推荐
- win向linux传文件
使用pscp.exe即可. 下载pscp.exe(http://pan.baidu.com/s/1jG6zmx4) 复制到windows/system32目录下即可. 然后可在cmd命令行下使用psc ...
- Haproxy配置参数
HAProxy配置中分成五部分内容,当然这些组件不是必选的,可以根据需要选择部分作为配置. ===================== global 参数是进程级的,通常和操作系统(OS)相关. ...
- 页面嵌套 Iframe 产生缓存导致页面数据不刷新问题
最近遇到个比较古怪的问题:当页面嵌套多个 Iframe 时会出现 Iframe 里包含的页面无法看到最新的页面信息. 初步解决方案,在 Iframe 指向的页面地址后缀添加一个随机数或者时间戳.这样能 ...
- 跳转界面方法 (runtime实用篇一)
在开发项目中,会有这样变态的需求: 推送:根据服务端推送过来的数据规则,跳转到对应的控制器 feeds列表:不同类似的cell,可能跳转不同的控制器(嘘!产品经理是这样要求:我也不确定会跳转哪个界面哦 ...
- asp:DateDiff 函数
DateDiff 函数 返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目. 语法 DateDiff(interval, date1, date2[, firstdayofwe ...
- ibatis+spring+cxf+mysql搭建webservice
首先需必备:mysql.myeclipse6.5.apache-cxf-2.6.2 一.建数据库,库名:cxf_demo:表名:book CREATE DATABASE `cxf_demo` --数 ...
- 基于SSM框架的简易的分页功能——包含maven项目的搭建
新人第一次发帖,有什么不对的地方请多多指教~~ 分页这个功能经常会被使用到,我之前学习的时候找了很多资源,可都看不懂(笨死算了),最后还是在朋友帮助下做出了这个分页.我现在把我所能想到的知识 做了一个 ...
- ASP.NET前端语法应用
字符拼接 <%# "abc" + Eval("列名").ToString() %> <%# Eval("列名"," ...
- Java LoggingAPI 使用方法
因为不想导入Log4j的jar,项目只是测试一些东西,因此选用了JDK 自带的Logging,这对于一些小的项目或者自己测试一些东西是比较好的选择. Log4j中是通过log4j.properties ...
- linux相关解压命令
ZIP 我们可以使用下列的命令压缩一个目录: # zip -r archive_name.zip directory_to_compress 下面是如果解压一个zip文档: # unzip archi ...