1.GZipStream 类

此类在 .NET Framework 2.0 版中是新增的。

提供用于压缩和解压缩流的方法和属性

2.压缩byte[]

  1. /// <summary>
  2. /// 压缩数据
  3. /// </summary>
  4. /// <param name="data"></param>
  5. /// <returns></returns>
  6. public byte[] Compress(byte[] data)
  7. {
  8. MemoryStream ms = new MemoryStream();
  9. GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress);
  10. zipStream.Write(data, 0, data.Length);//将数据压缩并写到基础流中
  11. zipStream.Close();
  12. return ms.ToArray();
  13. }

3.解压byte[]

  1. /// 解压数据
  2. /// </summary>
  3. /// <param name="data"></param>
  4. /// <returns></returns>
  5. public byte[] Decompress(byte[] data)
  6. {
  7. MemoryStream srcMs = new MemoryStream(data);
  8. GZipStream zipStream = new GZipStream(srcMs, CompressionMode.Decompress);
  9. MemoryStream ms = new MemoryStream();
  10. byte[] bytes = new byte[40960];
  11. int n;
  12. while ((n = zipStream.Read(bytes, 0, bytes.Length)) > 0)
  13. {
  14. ms.Write(bytes, 0, n);
  15. }
  16. zipStream.Close();
  17. return ms.ToArray();
  18. }

4.压缩byte[]数据,存放到文件中

  1. /// <summary>
  2. /// 将指定的字节数组压缩,并写入到目标文件
  3. /// </summary>
  4. /// <param name="srcBuffer">指定的源字节数组</param>
  5. /// <param name="destFile">指定的目标文件</param>
  6. public static void CompressData(byte[] srcBuffer, string destFile)
  7. {
  8. FileStream destStream = null;
  9. GZipStream compressedStream = null;
  10. try
  11. {
  12. //打开文件流
  13. destStream = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write);
  14. //指定压缩的目的流(这里是文件流)
  15. compressedStream = new GZipStream(destStream, CompressionMode.Compress, true);
  16. //往目的流中写数据,而流将数据写到指定的文件
  17. compressedStream.Write(srcBuffer, 0, srcBuffer.Length);
  18. }
  19. catch (Exception ex)
  20. {
  21. throw new Exception(String.Format("压缩数据写入文件{0}时发生错误", destFile), ex);
  22. }
  23. finally
  24. {
  25. // Make sure we allways close all streams
  26. if (null != compressedStream)
  27. {
  28. compressedStream.Close();
  29. compressedStream.Dispose();
  30. }
  31. if (null != destStream)
  32. destStream.Close();
  33. }
  34. }

5.解压文件,得到byte[]数据

  1. /// <summary>
  2. /// 将指定的文件解压,返回解压后的数据
  3. /// </summary>
  4. /// <param name="srcFile">指定的源文件</param>
  5. /// <returns>解压后得到的数据</returns>
  6. public static byte[] DecompressData(string srcFile)
  7. {
  8. if (false == File.Exists(srcFile))
  9. throw new FileNotFoundException(String.Format("找不到指定的文件{0}", srcFile));
  10. FileStream sourceStream = null;
  11. GZipStream decompressedStream = null;
  12. byte[] quartetBuffer = null;
  13. try
  14. {
  15. sourceStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
  16. decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);
  17. // Read the footer to determine the length of the destiantion file
  18. //GZIP文件格式说明:
  19. //10字节的头,包含幻数、版本号以及时间戳
  20. //可选的扩展头,如原文件名
  21. //文件体,包括DEFLATE压缩的数据
  22. //8字节的尾注,包括CRC-32校验和以及未压缩的原始数据长度(4字节) 文件大小不超过4G
  23. //为Data指定byte的长度,故意开大byte数据的范围
  24. //读取未压缩的原始数据长度
  25. quartetBuffer = new byte[4];
  26. long position = sourceStream.Length - 4;
  27. sourceStream.Position = position;
  28. sourceStream.Read(quartetBuffer, 0, 4);
  29. int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
  30. byte[] data;
  31. if (checkLength <= sourceStream.Length)
  32. {
  33. data = new byte[Int16.MaxValue];
  34. }
  35. else
  36. {
  37. data = new byte[checkLength + 100];
  38. }
  39. //每100byte从解压流中读出数据,并将读出的数据Copy到Data byte[]中,这样就完成了对数据的解压
  40. byte[] buffer = new byte[100];
  41. sourceStream.Position = 0;
  42. int offset = 0;
  43. int total = 0;
  44. while (true)
  45. {
  46. int bytesRead = decompressedStream.Read(buffer, 0, 100);
  47. if (bytesRead == 0)
  48. break;
  49. buffer.CopyTo(data, offset);
  50. offset += bytesRead;
  51. total += bytesRead;
  52. }
  53. //剔除多余的byte
  54. byte[] actualdata = new byte[total];
  55. for (int i = 0; i < total; i++)
  56. actualdata[i] = data[i];
  57. return actualdata;
  58. }
  59. catch (Exception ex)
  60. {
  61. throw new Exception(String.Format("从文件{0}解压数据时发生错误", srcFile), ex);
  62. }
  63. finally
  64. {
  65. if (sourceStream != null)
  66. sourceStream.Close();
  67. if (decompressedStream != null)
  68. decompressedStream.Close();
  69. }
  70. }

6.小结

压缩,解压都用GZipStream,操作的对象时普通流MemoryStream,不同的是:

压缩是将btye[]型的数据写入GZipStream中,而解压的时候是将GzipStream中的数据写入到byte[]中,并将读出的数据写入到MemoryStream后一次性输出

压缩到文件与压缩成byte[]不同的是压缩到文件利用到了FileStream将流写到文件,解压Gzip文件,需要根据文件的规则进行:后4位记录未压缩前的长度,根据该长度可以将解压出来的文件存放到稍大的byte[]中

.net 利用 GZipStream 压缩和解压缩的更多相关文章

  1. 利用ICSharpCode进行压缩和解压缩

    说说我利用ICSharpCode进行压缩和解压缩的一些自己的一下实践过程 1:组件下载地址 参考文章:C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件 2: 文件类 // ...

  2. C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件

    我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...

  3. 在C#中利用SharpZipLib进行文件的压缩和解压缩收藏

    我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net(http://www.icsharpcode.net/OpenSource/SharpZipL ...

  4. 利用SharpZipLib进行字符串的压缩和解压缩

    http://www.izhangheng.com/sharpziplib-string-compression-decompression/ 今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用 ...

  5. C#利用SharpZipLib进行文件的压缩和解压缩

    我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...

  6. C#- 压缩和解压缩的研究 .

    用了第二种方法,感觉很不错,其他都没用过了.摘录下来,做一个备忘. 最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip ...

  7. .net中压缩和解压缩的处理

    最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip 参考代码如下: //======================= ...

  8. 关于webservice大数据量传输时的压缩和解压缩

    当访问WebSerivice时,如果数据量很大,传输数据时就会很慢.为了提高速度,我们就会想到对数据进行压缩.首先我们来分析一下. 当在webserice中传输数据时,一般都采用Dataset进行数据 ...

  9. iOS中使用ZipArchive压缩和解压缩文件-备

    为什么我需要解压缩文件 有许多原因能解释为什么我要在工程中使用压缩和解压缩功能,下面是几个常见的原因: 苹果App Store的50M下载限制 苹 果公司出于流量的考虑,规定在非WIFI环境下,限制用 ...

随机推荐

  1. win向linux传文件

    使用pscp.exe即可. 下载pscp.exe(http://pan.baidu.com/s/1jG6zmx4) 复制到windows/system32目录下即可. 然后可在cmd命令行下使用psc ...

  2. Haproxy配置参数

    HAProxy配置中分成五部分内容,当然这些组件不是必选的,可以根据需要选择部分作为配置. ===================== global    参数是进程级的,通常和操作系统(OS)相关. ...

  3. 页面嵌套 Iframe 产生缓存导致页面数据不刷新问题

    最近遇到个比较古怪的问题:当页面嵌套多个 Iframe 时会出现 Iframe 里包含的页面无法看到最新的页面信息. 初步解决方案,在 Iframe 指向的页面地址后缀添加一个随机数或者时间戳.这样能 ...

  4. 跳转界面方法 (runtime实用篇一)

    在开发项目中,会有这样变态的需求: 推送:根据服务端推送过来的数据规则,跳转到对应的控制器 feeds列表:不同类似的cell,可能跳转不同的控制器(嘘!产品经理是这样要求:我也不确定会跳转哪个界面哦 ...

  5. asp:DateDiff 函数

    DateDiff 函数 返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目. 语法 DateDiff(interval, date1, date2[, firstdayofwe ...

  6. ibatis+spring+cxf+mysql搭建webservice

    首先需必备:mysql.myeclipse6.5.apache-cxf-2.6.2 一.建数据库,库名:cxf_demo:表名:book CREATE DATABASE `cxf_demo`  --数 ...

  7. 基于SSM框架的简易的分页功能——包含maven项目的搭建

    新人第一次发帖,有什么不对的地方请多多指教~~ 分页这个功能经常会被使用到,我之前学习的时候找了很多资源,可都看不懂(笨死算了),最后还是在朋友帮助下做出了这个分页.我现在把我所能想到的知识 做了一个 ...

  8. ASP.NET前端语法应用

    字符拼接 <%# "abc" + Eval("列名").ToString() %> <%# Eval("列名"," ...

  9. Java LoggingAPI 使用方法

    因为不想导入Log4j的jar,项目只是测试一些东西,因此选用了JDK 自带的Logging,这对于一些小的项目或者自己测试一些东西是比较好的选择. Log4j中是通过log4j.properties ...

  10. linux相关解压命令

    ZIP 我们可以使用下列的命令压缩一个目录: # zip -r archive_name.zip directory_to_compress 下面是如果解压一个zip文档: # unzip archi ...