国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

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

2019-10-29 21:09:44
字體:
來源:轉載
供稿:網(wǎng)友

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

使用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#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 土默特左旗| 厦门市| 武汉市| 达州市| 新田县| 松阳县| 望谟县| 紫金县| 县级市| 凤庆县| 东明县| 建水县| 梧州市| 积石山| 博罗县| 上蔡县| 刚察县| 革吉县| 青铜峡市| 兴国县| 松溪县| 华亭县| 谷城县| 盐津县| 嫩江县| 芦山县| 南郑县| 湘潭市| 辽宁省| 长葛市| 楚雄市| 玉溪市| 日照市| 周口市| 清流县| 赞皇县| 盐津县| 叶城县| 建瓯市| 沂水县| 沂水县|