[C#] C#自定义字符串压缩和解压缩代码库 →→→→→进入此内容的聊天室

来自 , 2020-08-21, 写在 C#, 查看 105 次.
URL http://www.code666.cn/view/084afd91
  1. class ZipLib
  2.     {
  3.         public static string Zip(string value)
  4.         {
  5.             //Transform string into byte[]  
  6.             byte[] byteArray = new byte[value.Length];
  7.             int indexBA = 0;
  8.             foreach (char item in value.ToCharArray())
  9.             {
  10.                 byteArray[indexBA++] = (byte)item;
  11.             }
  12.  
  13.             //Prepare for compress
  14.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  15.             System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
  16.                 System.IO.Compression.CompressionMode.Compress);
  17.  
  18.             //Compress
  19.             sw.Write(byteArray, 0, byteArray.Length);
  20.             //Close, DO NOT FLUSH cause bytes will go missing...
  21.             sw.Close();
  22.  
  23.             //Transform byte[] zip data to string
  24.             byteArray = ms.ToArray();
  25.             System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
  26.             foreach (byte item in byteArray)
  27.             {
  28.                 sB.Append((char)item);
  29.             }
  30.             ms.Close();
  31.             sw.Dispose();
  32.             ms.Dispose();
  33.             return sB.ToString();
  34.         }
  35.  
  36.         public static string UnZip(string value)
  37.         {
  38.             //Transform string into byte[]
  39.             byte[] byteArray = new byte[value.Length];
  40.             int indexBA = 0;
  41.             foreach (char item in value.ToCharArray())
  42.             {
  43.                 byteArray[indexBA++] = (byte)item;
  44.             }
  45.  
  46.             //Prepare for decompress
  47.             System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
  48.             System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
  49.                 System.IO.Compression.CompressionMode.Decompress);
  50.  
  51.             //Reset variable to collect uncompressed result
  52.             byteArray = new byte[byteArray.Length];
  53.  
  54.             //Decompress
  55.             int rByte = sr.Read(byteArray, 0, byteArray.Length);
  56.  
  57.             //Transform byte[] unzip data to string
  58.             System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
  59.             //Read the number of bytes GZipStream red and do not a for each bytes in
  60.             //resultByteArray;
  61.             for (int i = 0; i < rByte; i++)
  62.             {
  63.                 sB.Append((char)byteArray[i]);
  64.             }
  65.             sr.Close();
  66.             ms.Close();
  67.             sr.Dispose();
  68.             ms.Dispose();
  69.             return sB.ToString();
  70.         }
  71.     }
  72.  
  73.  
  74. //csharp/8280

回复 "C#自定义字符串压缩和解压缩代码库"

这儿你可以回复上面这条便签

captcha