C# 对字符串的压缩和解压

 
  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.     }  
  1. string str_org="aaaaaaaaaabbbbbbbbbbbbcccccccccdddddddd";   
  2.             string str_comp = ZipLib.Zip(str_org);   
  3.             Console.WriteLine("str_comp:" + str_comp);   
  4.   
  5.             string str_uncomp = ZipLib.UnZip(str_comp);   
  6.             Console.WriteLine("str_uncomp:" + str_uncomp);   
  7.   
  8.   
  9.             Console.ReadLine();  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值