| | |
| | | |
| | | } |
| | | |
| | | 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 解压缩字节数组 |
| | | //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 解压缩字节数组 |
| | | // 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; |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | } |