服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现压缩和解压缩的方法示例【Gzip和Zip方式】

C#实现压缩和解压缩的方法示例【Gzip和Zip方式】

2022-01-07 13:48_iorilan C#

这篇文章主要介绍了C#实现压缩和解压缩的方法,结合具体实例形式分析了Gzip和Zip两种压缩操作实现方法,需要的朋友可以参考下

本文实例讲述了C#实现压缩和解压缩的方法。分享给大家供大家参考,具体如下:

使用ICSharpCode.SharpZipLib.dll来压缩/解压(压缩效率比GZip要高一点)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public static class ZipUtil
{
    /// <summary>
    /// 压缩
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public static string Compress(string param)
    {
      byte[] data = System.Text.Encoding.UTF8.GetBytes(param);
      //byte[] data = Convert.FromBase64String(param);
      MemoryStream ms = new MemoryStream();
      Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms);
      try
      {
        stream.Write(data, 0, data.Length);
      }
      finally
      {
        stream.Close();
        ms.Close();
      }
      return Convert.ToBase64String(ms.ToArray());
    }
    /// <summary>
    /// 解压
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public static string Decompress(string param)
    {
      string commonString = "";
      byte[] buffer = Convert.FromBase64String(param);
      MemoryStream ms = new MemoryStream(buffer);
      Stream sm = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms);
      //这里要指明要读入的格式,要不就有乱码
      StreamReader reader = new StreamReader(sm, System.Text.Encoding.UTF8);
      try
      {
        commonString = reader.ReadToEnd();
      }
      finally
      {
        sm.Close();
        ms.Close();
      }
      return commonString;
    }
}

使用GZip来压缩/解压缩(字符串)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public static class GZipUtil
{
    public static string Zip(string value)
    {
      //Transform string into byte[]
      byte[] byteArray = new byte[value.Length];
      int indexBA = 0;
      foreach (char item in value.ToCharArray())
      {
        byteArray[indexBA++] = (byte)item;
      }
      //Prepare for compress
      System.IO.MemoryStream ms = new System.IO.MemoryStream();
      System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
        System.IO.Compression.CompressionMode.Compress);
      //Compress
      sw.Write(byteArray, 0, byteArray.Length);
      //Close, DO NOT FLUSH cause bytes will go missing...
      sw.Close();
      //Transform byte[] zip data to string
      byteArray = ms.ToArray();
      System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
      foreach (byte item in byteArray)
      {
        sB.Append((char)item);
      }
      ms.Close();
      sw.Dispose();
      ms.Dispose();
      return sB.ToString();
    }
    public static string UnZip(string value)
    {
      //Transform string into byte[]
      byte[] byteArray = new byte[value.Length];
      int indexBA = 0;
      foreach (char item in value.ToCharArray())
      {
        byteArray[indexBA++] = (byte)item;
      }
      //Prepare for decompress
      System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
      System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
        System.IO.Compression.CompressionMode.Decompress);
      //Reset variable to collect uncompressed result
      byteArray = new byte[byteArray.Length];
      //Decompress
      int rByte = sr.Read(byteArray, 0, byteArray.Length);
      //Transform byte[] unzip data to string
      System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
      //Read the number of bytes GZipStream red and do not a for each bytes in
      //resultByteArray;
      for (int i = 0; i < rByte; i++)
      {
        sB.Append((char)byteArray[i]);
      }
      sr.Close();
      ms.Close();
      sr.Dispose();
      ms.Dispose();
      return sB.ToString();
    }
}

希望本文所述对大家C#程序设计有所帮助。

延伸 · 阅读

精彩推荐
  • C#C#实现XML文件读取

    C#实现XML文件读取

    这篇文章主要为大家详细介绍了C#实现XML文件读取的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Just_for_Myself6702022-02-22
  • C#Unity3D实现虚拟按钮控制人物移动效果

    Unity3D实现虚拟按钮控制人物移动效果

    这篇文章主要为大家详细介绍了Unity3D实现虚拟按钮控制人物移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    shenqingyu060520232410972022-03-11
  • C#WPF 自定义雷达图开发实例教程

    WPF 自定义雷达图开发实例教程

    这篇文章主要介绍了WPF 自定义雷达图开发实例教程,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下...

    WinterFish13112021-12-06
  • C#C# 实现对PPT文档加密、解密及重置密码的操作方法

    C# 实现对PPT文档加密、解密及重置密码的操作方法

    这篇文章主要介绍了C# 实现对PPT文档加密、解密及重置密码的操作方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    E-iceblue5012022-02-12
  • C#深入解析C#中的交错数组与隐式类型的数组

    深入解析C#中的交错数组与隐式类型的数组

    这篇文章主要介绍了深入解析C#中的交错数组与隐式类型的数组,隐式类型的数组通常与匿名类型以及对象初始值设定项和集合初始值设定项一起使用,需要的...

    C#教程网6172021-11-09
  • C#C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例

    C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例

    这篇文章主要介绍了C#设计模式之Visitor访问者模式解决长隆欢乐世界问题,简单描述了访问者模式的定义并结合具体实例形式分析了C#使用访问者模式解决长...

    GhostRider9502022-01-21
  • C#C#裁剪,缩放,清晰度,水印处理操作示例

    C#裁剪,缩放,清晰度,水印处理操作示例

    这篇文章主要为大家详细介绍了C#裁剪,缩放,清晰度,水印处理操作示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    吴 剑8332021-12-08
  • C#C#通过KD树进行距离最近点的查找

    C#通过KD树进行距离最近点的查找

    这篇文章主要为大家详细介绍了C#通过KD树进行距离最近点的查找,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    帆帆帆6112022-01-22