欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

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

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

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

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

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来压缩/解压缩(字符串)

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#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》

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

上一篇:C#开发微信门户及应用(3) 文本消息和图文消息应答

栏    目:C#教程

下一篇:C# networkcomms 3.0实现模拟登陆总结

本文标题:C#实现压缩和解压缩的方法示例【Gzip和Zip方式】

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5670.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有