C#自定义字符串压缩和解压缩源码库
生活随笔
收集整理的這篇文章主要介紹了
C#自定义字符串压缩和解压缩源码库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如下的內容是關于C#自定義字符串壓縮和解壓縮庫的內容。class ZipLib{public static string Zip(string value){byte[] byteArray = new byte[value.Length];int indexBA = 0;foreach (char item in value.ToCharArray()){byteArray[indexBA++] = (byte)item;}System.IO.MemoryStream ms = new System.IO.MemoryStream();System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,System.IO.Compression.CompressionMode.Compress);sw.Write(byteArray, 0, byteArray.Length);sw.Close();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){byte[] byteArray = new byte[value.Length];int indexBA = 0;foreach (char item in value.ToCharArray()){byteArray[indexBA++] = (byte)item;}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);byteArray = new byte[byteArray.Length];int rByte = sr.Read(byteArray, 0, byteArray.Length);System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);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#自定义字符串压缩和解压缩源码库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 那些影响深远的弯路
- 下一篇: 读名老中医之路笔记(二)