最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#自定義字符串壓縮和解壓縮的方法

 更新時(shí)間:2015年04月01日 10:32:24   作者:lele  
這篇文章主要介紹了C#自定義字符串壓縮和解壓縮的方法,通過自定義C#字符串操作類實(shí)現(xiàn)對字符串的壓縮與解壓的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#自定義字符串壓縮和解壓縮的方法。分享給大家供大家參考。具體如下:

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

代碼使用方法:

string str_org="aaaaaaaaaabbbbbbbbbbbbcccccccccdddddddd";
string str_comp = ZipLib.Zip(str_org);
Console.WriteLine("str_comp:" + str_comp);
string str_uncomp = ZipLib.UnZip(str_comp);
Console.WriteLine("str_uncomp:" + str_uncomp);
Console.ReadLine();

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

相關(guān)文章

最新評論

林口县| 高密市| 内江市| 安庆市| 济阳县| 昂仁县| 阿克苏市| 香港| 启东市| 哈尔滨市| 托里县| 东城区| 曲靖市| 休宁县| 筠连县| 海宁市| 灵丘县| 噶尔县| 中牟县| 敦化市| 获嘉县| 高清| 鹤壁市| 铅山县| 涪陵区| 陆良县| 格尔木市| 苏尼特左旗| 晋江市| 崇文区| 正定县| 胶南市| 峨眉山市| 乐业县| 河北省| 肇东市| 邯郸县| 将乐县| 潜山县| 宣武区| 巧家县|