欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】

 更新時(shí)間:2017年06月12日 11:13:37   作者:_iorilan  
這篇文章主要介紹了C#實(shí)現(xiàn)壓縮和解壓縮的方法,結(jié)合具體實(shí)例形式分析了Gzip和Zip兩種壓縮操作實(shí)現(xiàn)方法,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)壓縮和解壓縮的方法。分享給大家供大家參考,具體如下:

使用ICSharpCode.SharpZipLib.dll來壓縮/解壓(壓縮效率比GZip要高一點(diǎn))

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();
    }
}

更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C# 多線程編程技術(shù)基礎(chǔ)知識(shí)入門

    C# 多線程編程技術(shù)基礎(chǔ)知識(shí)入門

    這篇文章主要介紹了C# 多線程編程技術(shù)基礎(chǔ)知識(shí),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2020-02-02
  • C#中AS和IS關(guān)鍵字的用法

    C#中AS和IS關(guān)鍵字的用法

    這篇文章主要介紹了C#中AS和IS關(guān)鍵字的用法的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • 基于Json序列化和反序列化通用的封裝完整代碼

    基于Json序列化和反序列化通用的封裝完整代碼

    JSON 是存儲(chǔ)和交換文本信息的語法。類似 XML。JSON 比 XML 更小、更快,更易解析。下面通過實(shí)例代碼給大家分享Json序列化和反序列化通用的封裝,需要的的朋友參考下吧
    2017-07-07
  • C# FileStream復(fù)制大文件

    C# FileStream復(fù)制大文件

    這篇文章主要為大家詳細(xì)介紹了C# FileStream復(fù)制大文件的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 使用C#編寫兩個(gè)漂亮?xí)r鐘的示例代碼

    使用C#編寫兩個(gè)漂亮?xí)r鐘的示例代碼

    這篇文章主要為大家分享了兩個(gè)使用C#編寫的兩個(gè)漂亮?xí)r鐘的示例代碼,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-07-07
  • C# 中類型轉(zhuǎn)換方式之顯式轉(zhuǎn)換和 as 運(yùn)算符

    C# 中類型轉(zhuǎn)換方式之顯式轉(zhuǎn)換和 as 運(yùn)算符

    在 C# 中,有兩種常見的類型轉(zhuǎn)換方式:顯式轉(zhuǎn)換和as 運(yùn)算符,它們用于在不同類型之間進(jìn)行轉(zhuǎn)換,以下是對(duì)這兩種轉(zhuǎn)換方式的詳細(xì)解釋和示例說明,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • c# Graphics使用方法(畫圓寫字代碼)

    c# Graphics使用方法(畫圓寫字代碼)

    本文主要介紹了Graphics的使用方法,提供如何畫圓、寫字的代碼,大家參考使用吧
    2014-01-01
  • C#中值類型和引用類型的區(qū)別深度分析

    C#中值類型和引用類型的區(qū)別深度分析

    這篇文章主要介紹了C#中值類型和引用類型的區(qū)別深度分析,以通俗易懂的語言形象化的分析了C#中值類型和引用類型的區(qū)別,對(duì)于深入理解C#數(shù)據(jù)類型有著不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-11-11
  • C#中string用法實(shí)例詳解

    C#中string用法實(shí)例詳解

    這篇文章主要介紹了C#中string用法,非常詳細(xì)的總結(jié)了比較常見的關(guān)于C#中string的幾個(gè)常用方法,需要的朋友可以參考下
    2014-08-08
  • C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式

    C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式

    學(xué)完設(shè)計(jì)模式之后,你就感覺它會(huì)慢慢地影響到你寫代碼的思維方式,下面這篇文章主要給大家介紹了關(guān)于C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式的相關(guān)資料,需要的朋友可以參考下
    2021-08-08

最新評(píng)論