From c0f9915265878e56e91ee97f7f8d925db1e12626 Mon Sep 17 00:00:00 2001 From: cloudflight <cloudflight@126.com> Date: 星期六, 02 十二月 2023 18:50:45 +0800 Subject: [PATCH] 2023年12月2日_3 --- Hydro.CommonBase/Helper/Base64Helper.cs | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 179 insertions(+), 0 deletions(-) diff --git a/Hydro.CommonBase/Helper/Base64Helper.cs b/Hydro.CommonBase/Helper/Base64Helper.cs index e2ea9c2..fae5ce7 100644 --- a/Hydro.CommonBase/Helper/Base64Helper.cs +++ b/Hydro.CommonBase/Helper/Base64Helper.cs @@ -78,4 +78,183 @@ } + public static class MessageCompressHelper + { + public static void SaveCompressedBase64ToFile<T>(T obj, string filePath) + { + // 灏嗗璞″簭鍒楀寲涓哄瓧鑺傛暟缁� + var formatter = MessagePackSerializer.Serialize<T>(obj); + //var obj1 = MessagePackSerializer.Deserialize<T>(formatter); + File.WriteAllBytes(filePath, formatter); + //byte[] compressedData; + + //// 浣跨敤 GZip 鍘嬬缉瀛楄妭鏁扮粍 + //using (var outputStream = new MemoryStream()) + //{ + // using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress)) + // { + // outputStream.Write(formatter, 0, formatter.Length); + // } + // compressedData = outputStream.ToArray(); + //} + + //// 灏嗗帇缂╁悗鐨勫瓧鑺傛暟缁勮浆涓� Base64 瀛楃涓� + //var base64String = Convert.ToBase64String(compressedData); + + // 灏� Base64 瀛楃涓插啓鍏ユ枃浠� + //File.WriteAllText(filePath, base64String); + } + + public static T ReadCompressedBase64FromFile<T>(string filePath) + { + //// 浠庢枃浠朵腑璇诲彇 Base64 瀛楃涓� + //var base64String = File.ReadAllText(filePath); + + //// 灏� Base64 瀛楃涓茶浆涓哄帇缂╁悗鐨勫瓧鑺傛暟缁� + //byte[] compressedData = Convert.FromBase64String(base64String); + + //// 浣跨敤 GZip 瑙e帇缂╁瓧鑺傛暟缁� + //using (var inputStream = new MemoryStream(compressedData)) + //using (var outputStream = new MemoryStream()) + //{ + // using (var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress)) + // { + // gzipStream.CopyTo(outputStream); + // } + + // // 灏嗚В鍘嬬缉鍚庣殑瀛楄妭鏁扮粍鍙嶅簭鍒楀寲涓哄璞� + // var obj = MessagePackSerializer.Deserialize<T>(outputStream.ToArray()); + + // return obj; + //} + var formatter=File.ReadAllBytes(filePath ); + var obj = MessagePackSerializer.Deserialize<T>(formatter); + return obj; + + + } + + //public static void CopyMessage<T>(this object obj) + //{ + + // //return obj1; + //} + + + + } + + public class ProtoBufHelper + { + //public static void SaveCompressedProtoBufToFile<T>(T obj, string filePath) where T : IMessage<T> + //{ + // // 灏嗗璞″簭鍒楀寲涓哄瓧鑺傛暟缁� + // byte[] buffer = obj.ToByteArray(); + + // // 浣跨敤 GZip 鍘嬬缉瀛楄妭鏁扮粍 + // byte[] compressedData; + // using (var outputStream = new MemoryStream()) + // { + // using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress)) + // { + // gzipStream.Write(buffer, 0, buffer.Length); + // } + // compressedData = outputStream.ToArray(); + // } + + // // 灏嗗帇缂╁悗鐨勫瓧鑺傛暟缁勫啓鍏ユ枃浠� + // File.WriteAllBytes(filePath, compressedData); + //} + + //public static T ReadCompressedProtoBufFromFile<T>(string filePath) where T : IMessage<T>, new() + //{ + // // 浠庢枃浠朵腑璇诲彇瀛楄妭鏁扮粍 + // byte[] buffer = File.ReadAllBytes(filePath); + + // // 浣跨敤 GZip 瑙e帇缂╁瓧鑺傛暟缁� + // byte[] decompressedData; + // using (var inputStream = new MemoryStream(buffer)) + // using (var outputStream = new MemoryStream()) + // { + // using (var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress)) + // { + // gzipStream.CopyTo(outputStream); + // } + // decompressedData = outputStream.ToArray(); + // } + + // // 灏嗚В鍘嬬缉鍚庣殑瀛楄妭鏁扮粍鍙嶅簭鍒楀寲涓哄璞� + // T obj = new T(); + // obj.MergeFrom(decompressedData); + + // return obj; + //} + } + + + + //[Serializable] + //public class CDictionary<TKey, TValue> : Dictionary<TKey, TValue> + //{ + // public CDictionary() :base() { } + // public new void Add(TKey key, TValue value) + // { + // if (ContainsKey(key)) + // { + // // 澶勭悊閲嶅閿殑閫昏緫锛岃繖閲屼互瑕嗙洊鍘熸湁鍊间负渚� + // this[key] = value; + // } + // else + // { + // base.Add(key, value); + // } + // } + + // public void FromDictionary(Dictionary<TKey, TValue> dictionary) + // { + // //CustomDictionary<TKey, TValue> customDictionary = new CustomDictionary<TKey, TValue>(); + // this.Clear(); + // foreach (KeyValuePair<TKey, TValue> kvp in dictionary) + // { + // this.Add(kvp.Key, kvp.Value); + // } + // //return customDictionary; + + // } + + // public Dictionary<TKey, TValue> ToDictionary() + // { + // Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>(); + // foreach (KeyValuePair<TKey, TValue> kvp in this) + // { + // dictionary[kvp.Key] = kvp.Value; + // } + // return dictionary; + // } + //} + + + public static class ToDictionaryExtentions + { + public static ConcurrentDictionary<TKey, TValue> ToDictionaryEx<TElement, TKey, TValue>( + this IEnumerable<TElement> source, + Func<TElement, TKey> keyGetter, + Func<TElement, TValue> valueGetter) + { + ConcurrentDictionary<TKey, TValue> dict = new ConcurrentDictionary<TKey, TValue>(); // new Dictionary<TKey, TValue>(); + foreach (var e in source) + { + var key = keyGetter(e); + if (dict.ContainsKey(key)) + { + continue; + } + dict.TryAdd(key, valueGetter(e)); + } + return dict; + } + + + + } } -- Gitblit v1.9.3