using System; using System.Collections.Generic; using System.IO.Compression; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Collections.Concurrent; using MessagePack; using MessagePack.Resolvers; using System.Runtime.CompilerServices; using System.Runtime.Serialization; //using NPOI.SS.Formula.Functions; namespace CommonBase { public class Base64Helper { public static void SaveCompressedBase64ToFile(T obj, string filePath) { // 将对象序列化为字节数组 var formatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream()) { formatter.Serialize(memoryStream, obj); byte[] compressedData; // 使用 GZip 压缩字节数组 using (var outputStream = new MemoryStream()) { using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress)) { memoryStream.Position = 0; memoryStream.CopyTo(gzipStream); } compressedData = outputStream.ToArray(); } // 将压缩后的字节数组转为 Base64 字符串 var base64String = Convert.ToBase64String(compressedData); // 将 Base64 字符串写入文件 File.WriteAllText(filePath, base64String); } } public static T ReadCompressedBase64FromFile(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 formatter = new BinaryFormatter(); outputStream.Position = 0; var obj = (T)formatter.Deserialize(outputStream); return obj; } } } public static class MessageCompressHelper { public static void SaveCompressedBase64ToFile(T obj, string filePath) { // 将对象序列化为字节数组 var formatter = MessagePackSerializer.Serialize(obj); //var obj1 = MessagePackSerializer.Deserialize(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(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(outputStream.ToArray()); // return obj; //} var formatter=File.ReadAllBytes(filePath ); var obj = MessagePackSerializer.Deserialize(formatter); return obj; } //public static void CopyMessage(this object obj) //{ // //return obj1; //} } public class ProtoBufHelper { //public static void SaveCompressedProtoBufToFile(T obj, string filePath) where T : IMessage //{ // // 将对象序列化为字节数组 // 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(string filePath) where T : IMessage, 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 : Dictionary //{ // 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 dictionary) // { // //CustomDictionary customDictionary = new CustomDictionary(); // this.Clear(); // foreach (KeyValuePair kvp in dictionary) // { // this.Add(kvp.Key, kvp.Value); // } // //return customDictionary; // } // public Dictionary ToDictionary() // { // Dictionary dictionary = new Dictionary(); // foreach (KeyValuePair kvp in this) // { // dictionary[kvp.Key] = kvp.Value; // } // return dictionary; // } //} public static class ToDictionaryExtentions { public static ConcurrentDictionary ToDictionaryEx( this IEnumerable source, Func keyGetter, Func valueGetter) { ConcurrentDictionary dict = new ConcurrentDictionary(); // new Dictionary(); foreach (var e in source) { var key = keyGetter(e); if (dict.ContainsKey(key)) { continue; } dict.TryAdd(key, valueGetter(e)); } return dict; } } }