下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
C#对字符串进行压缩和解压缩的算法代码,字符串通过base64编码后传输public class Compression { /// <summary> /// 对字符串进行压缩 /// </summary> /// <param name="str">待压缩的字符串</param> /// <returns>压缩后的字符串</returns> public static string Compressstring(string str) { string compressstring = ""; byte[] compressBeforeByte = EnCoding.GetEnCoding("UTF-8").GetBytes(str); byte[] compressAfterByte=Compress(compressBeforeByte); //compressstring = EnCoding.GetEnCoding("UTF-8").GetString(compressAfterByte); compressstring = Convert.ToBase64String(compressAfterByte); return compressstring; } /// <summary> /// 对字符串进行解压缩 /// </summary> /// <param name="str">待解压缩的字符串</param> /// <returns>解压缩后的字符串</returns> public static string Decompressstring(string str) { string compressstring = ""; //byte[] compressBeforeByte = EnCoding.GetEnCoding("UTF-8").GetBytes(str); byte[] compressBeforeByte = Convert.FromBase64String(str); byte[] compressAfterByte = Decompress(compressBeforeByte); compressstring = EnCoding.GetEnCoding("UTF-8").GetString(compressAfterByte); return compressstring; } /// <summary> /// 对文件进行压缩 /// </summary> /// <param name="sourcefile">待压缩的文件名</param> /// <param name="destinationfile">压缩后的文件名</param> public static voID Compressfile(string sourcefile,string destinationfile) { throw new Exception("The method or operation is not implemented."); } /// <summary> /// 对文件进行解压缩 /// </summary> /// <param name="sourcefile">待解压缩的文件名</param> /// <param name="destinationfile">解压缩后的文件名</param> /// <returns></returns> public static voID Decompressfile(string sourcefile,string destinationfile) { throw new Exception("The method or operation is not implemented."); } /// <summary> /// 对byte数组进行压缩 /// </summary> /// <param name="data">待压缩的byte数组</param> /// <returns>压缩后的byte数组</returns> public static byte[] Compress(byte[] data) { try { MemoryStream ms = new MemoryStream(); GZipStream zip = new GZipStream(ms,CompressionMode.Compress,true); zip.Write(data,data.Length); zip.Close(); byte[] buffer = new byte[ms.Length]; ms.position=0; ms.Read(buffer,buffer.Length); ms.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); GZipStream zip = new GZipStream(ms,CompressionMode.Decompress,true); MemoryStream msreader = new MemoryStream(); byte[] buffer = new byte[0x1000]; while (true) { int reader = zip.Read(buffer,buffer.Length); if (reader <= 0) { break; } msreader.Write(buffer,reader); } zip.Close(); ms.Close(); msreader.position = 0; buffer = msreader.ToArray(); msreader.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } }
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的C#对字符串进行压缩和解压缩的算法代码全部内容,希望文章能够帮你解决C#对字符串进行压缩和解压缩的算法代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)