xx using System.IO.Compression;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | public class StringCompress { /// <summary> /// 字符串压缩 /// </summary> /// <param name="strSource"></param> /// <returns></returns> public static byte [] Compress( byte [] data) { MemoryStream ms = null ; GZipStream zip = null ; try { ms = new MemoryStream(); zip = new GZipStream(ms, CompressionMode.Compress, true ); zip.Write(data, 0, data.Length); zip.Close(); byte [] buffer = new byte [ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); ms.Close(); return buffer; } catch (Exception e) { Inf.DevLib.Diagnostics.AppTraceLog.WriteException(e); return null ; } finally { if (zip != null ) { zip.Close(); } if (ms != null ) { ms.Close(); } } } /// <summary> /// 字符串解压缩 /// </summary> /// <param name="strSource"></param> /// <returns></returns> public static byte [] Decompress( byte [] data) { MemoryStream ms = null ; GZipStream zip = null ; MemoryStream msreader = null ; try { ms = new MemoryStream(data); zip = new GZipStream(ms, CompressionMode.Decompress, true ); msreader = new MemoryStream(); byte [] buffer = new byte [0x1000]; while ( true ) { int reader = zip.Read(buffer, 0, buffer.Length); if (reader <= 0) { break ; } msreader.Write(buffer, 0, reader); } zip.Close(); ms.Close(); msreader.Position = 0; buffer = msreader.ToArray(); msreader.Close(); return buffer; } catch (Exception e) { Inf.DevLib.Diagnostics.AppTraceLog.WriteException(e); return null ; } finally { if (zip != null ) { zip.Close(); } if (ms != null ) { ms.Close(); } if (msreader != null ) { msreader.Close(); } } } public static string CompressString( string str) { string compressString = "" ; byte [] compressBeforeByte = Encoding.GetEncoding( "UTF-8" ).GetBytes(str); byte [] compressAfterByte = Compress(compressBeforeByte); if (compressAfterByte != null ) { compressString = Convert.ToBase64String(compressAfterByte); } return compressString; } public static string DecompressString( string str) { string compressString = "" ; byte [] compressBeforeByte = Convert.FromBase64String(str); byte [] compressAfterByte = Decompress(compressBeforeByte); if (compressAfterByte != null ) { compressString = Encoding.GetEncoding( "UTF-8" ).GetString(compressAfterByte); } return compressString; } /// <summary> /// 解压 /// </summary> /// <param name="param"></param> /// <returns></returns> public static string DecompressZlib( string param) { string commonString = "" ; byte [] buffer = Convert.FromBase64String(param); MemoryStream ms = new MemoryStream(buffer); Stream sm = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(ms); //这里要指明要读入的格式,要不就有乱码 StreamReader reader = new StreamReader(sm, System.Text.Encoding.UTF8); try { commonString = reader.ReadToEnd(); } finally { sm.Close(); ms.Close(); } return commonString; } } |
分类:
.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2018-12-10 ABP框架初始化数据(自定义)
2015-12-10 从客户端中检测到有潜在危险的 request.form值[解决方法]