首先需要下载SharpZipLib,下载地址:http://icsharpcode.github.io/SharpZipLib/

需要引入命名空间:

  1. using ICSharpCode.SharpZipLib.GZip;
  2. using System.IO;

压缩:

  1. public static byte[] CompressGZip(byte[] rawData)
  2. {
  3. MemoryStream ms = new MemoryStream();
  4. GZipOutputStream compressedzipStream = new GZipOutputStream(ms);
  5. compressedzipStream.Write(rawData, 0, rawData.Length);
  6. compressedzipStream.Close();
  7. return ms.ToArray();
  8. }

解压:

  1. public static byte[] UnGZip(byte[] byteArray)
  2. {
  3. GZipInputStream gzi = new GZipInputStream(new MemoryStream(byteArray));
  4. MemoryStream re = new MemoryStream(50000);
  5. int count;
  6. byte[] data = new byte[50000];
  7. while ((count = gzi.Read(data, 0, data.Length)) != 0)
  8. {
  9. re.Write(data, 0, count);
  10. }
  11. byte[] overarr = re.ToArray();
  12. return overarr;
  13. }

测试:

  1. public static void GZipTest()
  2. {
  3. string testdata = "aaaa11233GZip压缩和解压";
  4. byte[] gzipdata = Tools.CompressGZip(Encoding.UTF8.GetBytes(testdata));
  5. byte[] undata = Tools.UnGZip(gzipdata);
  6. Debug.Log("[GZipTest]  : data" + Encoding.UTF8.GetString(undata));
  7. }

结果:

c#使用SharpZipLib对二进制数据进行压缩和解压的更多相关文章

  1. C#实现通过Gzip来对数据进行压缩和解压

    C#实现通过Gzip来对数据进行压缩和解压 internal static byte[] Compress(byte[] data) { using (var compressedStream = n ...

  2. 使用zlib实现gzip格式数据的压缩和解压

    注意代码中的注释部分,这里设置是专门针对gzip的,缺少了就不行了,gzip压缩格式和其他格式的区别就在这里. Bytef 就是 unsigned char,uLong就是 unsigned long ...

  3. 利用c#自带的类对文件进行压缩和解压处理

    在做网络传输文件的小例子的时候,当传输的文件比较大的时候,我们通常都是将文件经过压缩之后才进行传输,以前都是利用第三方插件来对文件进行压缩的,但是现在我发现了c#自带的类库也能够实现文件的压缩,实际上 ...

  4. C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)

    我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...

  5. 对数据进行GZIP压缩和解压

    public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...

  6. [Swift通天遁地]七、数据与安全-(9)文件的压缩和解压

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. ZIP文件流压缩和解压

    前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...

  8. 【C#公共帮助类】WinRarHelper帮助类,实现文件或文件夹压缩和解压,实战干货

    关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩类博客,压缩的是zip文件htt ...

  9. .net文件压缩和解压及中文文件夹名称乱码问题

    /**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...

随机推荐

  1. 二、redis学习(java操作redis缓存的工具jedis)

  2. Mac安装PHP(Homebrew/php弃用、其他第三方tap也已经弃用或者迁移后的安装配置方案)

    一.前言 看网上很多资料,大多数都是 mac安装php,只需要: brew tap homebrew/php brew install phpXX 安装php扩展只需要: brew install p ...

  3. 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  4. html和css牛刀小试

    html和css网上教程很多,这里我也给大家一个网址:https://www.cnblogs.com/majj/ 今天心血来潮就模仿着小米的官网写了部分代码,效果图如下:(本人故意加了个华为广告栏在最 ...

  5. Mongodb操作-更新操作符

    1.$inc 用法:{$inc:{field:value}} 作用:对一个数字字段的某个field增加value 示例:将name为chenzhou的学生的age增加5 > db.student ...

  6. 杜教BM模板

    #include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #defi ...

  7. Hibernate的缓存(收集)

    (1)缓存就是把以前从数据库中查询出来和使用过的对象保存在内存中(一个数据结构中),这个数据结构通常是或类似Hashmap,当以后要使用某个对象 时,先查询缓存中是否有这个对象,如果有则使用缓存中的对 ...

  8. mysql每日数据统计

    select a.click_date from ( SELECT curdate() as click_date union all day) as click_date union all day ...

  9. Spring boot使用Redis时,报错,有redisTemplate和stringRedisTemplate两个bean?

    Error starting ApplicationContext. To display the auto-configuration report re-run your application ...

  10. HDU - 6396 Swordsman (单调性+贪心)

    题意:有n个怪物和k种属性,当且仅当你的每种属性都大于等于怪物的属性才可以击杀它,且击杀怪物可以提升你一定的属性值.求可击杀怪物的最大数量以最终的属性值. 这不就是银行家算法里的安全性检验么? 本题的 ...