| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Others;
- namespace SAGA.DotNetUtils.Serializer
- {
- public class SerializerByBinary
- {
- public static object DeSerializeBinary(MemoryStream memStream)
- {
- using (memStream)
- {
- memStream.Position = 0L;
- BinaryFormatter formatter = new BinaryFormatter {
- Binder = new UBinder("DLL")
- };
- return formatter.Deserialize(memStream);
- }
- }
- public static object DeSerializeBytes(byte[] value)
- {
- return ToObject(value);
- }
- public static object DeSerializeObject(string strSerializInfo)
- {
- if (strSerializInfo == null)
- {
- return null;
- }
- MemoryStream memStream = new MemoryStream(CompressHelper.DeCompress(strSerializInfo));
- return DeSerializeBinary(memStream);
- }
- public static object DeSerializeObjectNoneCompress(string strSerializInfo)
- {
- if (strSerializInfo == null)
- {
- return null;
- }
- return ToObject(Convert.FromBase64String(strSerializInfo));
- }
- public static object DeSerializeObjectSystemCompress(string strSerializInfo)
- {
- if (strSerializInfo == null)
- {
- return null;
- }
- byte[] buffer = new byte[100];
- MemoryStream stream = new MemoryStream(Convert.FromBase64String(strSerializInfo)) {
- Position = 0L
- };
- using (GZipStream stream2 = new GZipStream(stream, CompressionMode.Decompress))
- {
- using (MemoryStream stream3 = new MemoryStream())
- {
- int count = stream2.Read(buffer, 0, buffer.Length);
- while (count > 0)
- {
- stream3.Write(buffer, 0, count);
- count = stream2.Read(buffer, 0, buffer.Length);
- if (count <= 0)
- {
- break;
- }
- }
- stream3.Flush();
- buffer = stream3.ToArray();
- }
- }
- return ToObject(buffer);
- }
- public static object GetProjData(string strProjFile)
- {
- object obj2 = null;
- if (!File.Exists(strProjFile))
- {
- return null;
- }
- string str = DateTime.Now.ToStringExt("yyyy-MM-dd HH:mm:ss");
- string str2 = Path.Combine(Path.GetDirectoryName(strProjFile), str);
- string strTargetFile = Path.Combine(str2, Path.GetFileName(strProjFile));
- Directory.CreateDirectory(str2);
- CompressHelper.DeCompressFile(strProjFile, strTargetFile);
- IFormatter formatter = new BinaryFormatter {
- Binder = new UBinder("DLL")
- };
- Stream serializationStream = new FileStream(strTargetFile, FileMode.Open, FileAccess.Read, FileShare.Read);
- obj2 = formatter.Deserialize(serializationStream);
- serializationStream.Close();
- ComMethod.FileDelete(strTargetFile);
- ComMethod.FolderDelete(str2);
- return obj2;
- }
- public static object GetProjData2(string strProjFile, List<Type> listTypes)
- {
- object obj2 = null;
- if (!File.Exists(strProjFile))
- {
- return null;
- }
- IFormatter formatter = new BinaryFormatter {
- Binder = new UBinder(listTypes)
- };
- Stream serializationStream = new FileStream(strProjFile, FileMode.Open, FileAccess.Read, FileShare.Read);
- try
- {
- obj2 = formatter.Deserialize(serializationStream);
- }
- catch (Exception exception)
- {
- MessageShowBase.Show(exception);
- }
- finally
- {
- serializationStream.Close();
- }
- return obj2;
- }
- public static void SaveProjData(string strProjFile, object objProjData)
- {
- string str = DateTime.Now.ToStringExt("yyyy-MM-dd HH:mm:ss");
- string str2 = Path.Combine(Path.GetDirectoryName(strProjFile), str);
- string path = Path.Combine(str2, Path.GetFileName(strProjFile));
- Directory.CreateDirectory(str2);
- IFormatter formatter = new BinaryFormatter();
- Stream serializationStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
- formatter.Serialize(serializationStream, objProjData);
- serializationStream.Close();
- ComMethod.FileDelete(strProjFile);
- CompressHelper.CompressFile(path, strProjFile);
- ComMethod.FileDelete(path);
- ComMethod.FolderDelete(str2);
- }
- public static void SaveProjData2(string strProjFile, object objProjData)
- {
- IFormatter formatter = new BinaryFormatter();
- Stream serializationStream = new FileStream(strProjFile, FileMode.Create, FileAccess.Write, FileShare.None);
- try
- {
- formatter.Serialize(serializationStream, objProjData);
- }
- catch (Exception exception)
- {
- MessageShowBase.Show(exception);
- }
- finally
- {
- serializationStream.Close();
- }
- }
- public static MemoryStream SerializeBinary(object request)
- {
- if (request == null)
- {
- return null;
- }
- BinaryFormatter formatter = new BinaryFormatter {
- Binder = new UBinder("DLL")
- };
- MemoryStream serializationStream = new MemoryStream();
- formatter.Serialize(serializationStream, request);
- serializationStream.Flush();
- serializationStream.Position = 0L;
- return serializationStream;
- }
- public static byte[] SerializeBytes(object value)
- {
- return ToBytes(value);
- }
- public static string SerializeObject(object value)
- {
- if (value == null)
- {
- return null;
- }
- using (MemoryStream stream = SerializeBinary(value))
- {
- return CompressHelper.Compress(stream.ToArray());
- }
- }
- public static string SerializeObjectNoneCompress(object value)
- {
- if (value == null)
- {
- return null;
- }
- return Convert.ToBase64String(ToBytes(value));
- }
- public static string SerializeObjectSystemCompress(object value)
- {
- if (value == null)
- {
- return null;
- }
- byte[] buffer = ToBytes(value);
- using (MemoryStream stream = new MemoryStream())
- {
- using (GZipStream stream2 = new GZipStream(stream, CompressionMode.Compress, true))
- {
- stream2.Write(buffer, 0, buffer.Length);
- stream2.Flush();
- stream2.Close();
- }
- buffer = stream.ToArray();
- stream.Close();
- return Convert.ToBase64String(buffer);
- }
- }
- public static byte[] ToBytes(object value)
- {
- if (value == null)
- {
- return null;
- }
- using (MemoryStream stream = new MemoryStream())
- {
- new BinaryFormatter().Serialize(stream, value);
- return stream.ToArray();
- }
- }
- public static object ToObject(byte[] serializedObject)
- {
- if (serializedObject == null)
- {
- return null;
- }
- using (MemoryStream stream = new MemoryStream(serializedObject))
- {
- BinaryFormatter formatter = new BinaryFormatter {
- Binder = new UBinder("DLL")
- };
- return formatter.Deserialize(stream);
- }
- }
- public static object ToObject(byte[] serializedObject, List<Type> listTypes)
- {
- if (serializedObject == null)
- {
- return null;
- }
- using (MemoryStream stream = new MemoryStream(serializedObject))
- {
- BinaryFormatter formatter = new BinaryFormatter {
- Binder = new UBinder(listTypes)
- };
- return formatter.Deserialize(stream);
- }
- }
- }
- }
|