160 lines
5.0 KiB
C#
160 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.IO;
|
|
using Lskj.DevFx.ExceptionManagement;
|
|
using System.Runtime.Serialization;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace Lskj.DevFx.Serial
|
|
{
|
|
public static class SerialHelper
|
|
{
|
|
/// <summary>
|
|
/// 对对象进行二进制序列化后进行压缩
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static byte[] ObjectToBinary(object obj)
|
|
{
|
|
BinaryFormatter ser = new BinaryFormatter();
|
|
MemoryStream ms = new MemoryStream();
|
|
ser.Serialize(ms, obj);
|
|
byte[] buffer = ms.ToArray();
|
|
//ZIP压缩
|
|
byte[] Zipbuffer = ByteCompress.Compress(buffer);
|
|
//返回ZIP压缩后的字节数组
|
|
return Zipbuffer;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对对象进行二进制序列化后保存到指定的文件中
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static bool SaveToBinaryFile(object obj, string filepath)
|
|
{
|
|
return SaveToBinaryFile(obj, filepath, new BinaryFormatter());
|
|
}
|
|
/// <summary>
|
|
/// 对对象进行二进制序列化后保存到指定的文件中
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static bool SaveToBinaryFile(object obj, string filepath, IFormatter formatter)
|
|
{
|
|
try
|
|
{
|
|
using (FileStream fs = new FileStream(filepath, FileMode.Create))
|
|
{
|
|
formatter.Serialize(fs, obj);
|
|
return true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new BaseException(ex.Message,ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从文件中读出被序列化的对象
|
|
/// </summary>
|
|
/// <param name="bytDs"></param>
|
|
/// <returns></returns>
|
|
public static object ReadFormBinaryFile(string filepath)
|
|
{
|
|
return ReadFormBinaryFile(filepath, new BinaryFormatter());
|
|
}
|
|
/// <summary>
|
|
/// 从文件中读出被序列化的对象
|
|
/// </summary>
|
|
/// <param name="bytDs"></param>
|
|
/// <returns></returns>
|
|
public static object ReadFormBinaryFile(string filepath, IFormatter formatter)
|
|
{
|
|
try
|
|
{
|
|
using (FileStream fs = new FileStream(filepath, FileMode.Open))
|
|
{
|
|
return formatter.Deserialize(fs);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new BaseException(ex.Message, ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解压缩并转换Byte字节数组为对象
|
|
/// </summary>
|
|
/// <param name="bytDs"></param>
|
|
/// <returns></returns>
|
|
public static object BinaryToObject(byte[] zipbyte)
|
|
{
|
|
//先解压缩
|
|
byte[] buffer = ByteCompress.Decompress(zipbyte);
|
|
BinaryFormatter ser = new BinaryFormatter();
|
|
//后再反序列化成实际的对象
|
|
object obj = ser.Deserialize(new MemoryStream(buffer));
|
|
return obj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从XML文件中反序列化类
|
|
/// </summary>
|
|
/// <param name="type">类型</param>
|
|
/// <param name="filename">文件的绝对路径</param>
|
|
/// <returns></returns>
|
|
public static object ReadFormXmlFile(Type type, string filename)
|
|
{
|
|
FileStream fs = null;
|
|
try
|
|
{
|
|
// open the stream...
|
|
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
XmlSerializer serializer = new XmlSerializer(type);
|
|
return serializer.Deserialize(fs);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
if (fs != null)
|
|
fs.Close();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 序列化对象并以XML方式保存到指定的文件中
|
|
/// </summary>
|
|
/// <param name="obj">对象</param>
|
|
/// <param name="filename">文件路径</param>
|
|
public static void SaveToXmlFile(object obj, string filename)
|
|
{
|
|
FileStream fs = null;
|
|
try
|
|
{
|
|
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
|
|
XmlSerializer serializer = new XmlSerializer(obj.GetType());
|
|
serializer.Serialize(fs, obj);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
if (fs != null)
|
|
fs.Close();
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|