服务器之家

服务器之家 > 正文

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

时间:2022-01-07 13:48     来源/作者:_iorilan

本文实例讲述了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#程序设计有所帮助。

标签:

相关文章

热门资讯

蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部