using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
namespace Dal
{
public class ClassAndByte
{
/// <summary>
/// 将可序列化类的对象转换为byte[]
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static byte[] ObjectToBytes<T>(T obj)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
return ms.GetBuffer();
}
}
catch (Exception ex)
{
throw ex;
}
}
/**/
/// <summary>
/// 将一个序列化后的byte[]数组还原
/// </summary>
/// <param name="Bytes"></param>
/// <returns></returns>
public static T BytesToObject<T>(byte[] arrBytes)
{
try
{
using (MemoryStream ms = new MemoryStream(arrBytes))
{
IFormatter formatter = new BinaryFormatter();
object obj = formatter.Deserialize(ms);
T t = (T)obj;
return t;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将类对象转换为byte数组,你可以设置isCompress参数,要求数据是否被压缩
/// </summary>
/// <typeparam name="T">对象的类型</typeparam>
/// <param name="obj">要转换为byte的对象</param>
/// <param name="isCompress">是否要压缩数据</param>
/// <returns>转换后的byte数组</returns>
public static byte[] ObjectToBytes<T>(T obj,bool isCompress)
{
if (!isCompress)
{
return ObjectToBytes<T>(obj);
}
else
{
try
{
using (MemoryStream ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
byte[] arrByte = ms.GetBuffer();
byte[] arrByteCompress = Compress(arrByte);
return arrByteCompress;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// 将byte数组转换为对象,,如果数据是压缩数据,那么isCompress必须为true
/// </summary>
/// <typeparam name="T">对象的类型</typeparam>
/// <param name="arrBytes">要转换的数组</param>
/// <param name="isCompress">数据有没有压缩过</param>
/// <returns>byte数组转换后的类对象</returns>
public static T BytesToObject<T>(byte[] arrBytes,bool isCompress)
{
if (!isCompress)
{
return BytesToObject<T>(arrBytes);
}
else
{
try
{
byte[] arrByteDecompress = Decompress(arrBytes);
using (MemoryStream ms = new MemoryStream(arrByteDecompress))
{
IFormatter formatter = new BinaryFormatter();
object obj = formatter.Deserialize(ms);
T t = (T)obj;
return t;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
//-------------------------------------------------------------------------------
/// <summary>
/// 将可序列化对象转成Byte数组
/// </summary>
/// <param name="o">对象</param>
/// <returns>返回相关数组</returns>
protected static byte[] ObjectToByteArray(object obj)
{
using (MemoryStream ms = new MemoryStream())
{
IFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
/// <summary>
/// 将可序列化对象转成的byte数组还原为对象
/// </summary>
/// <param name="b">byte数组</param>
/// <returns>相关对象</returns>
protected static object ByteArrayToObject(byte[] arrBytes)
{
using (MemoryStream ms = new MemoryStream(arrBytes, 0, arrBytes.Length))
{
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(ms);
}
}
/// <summary>
/// 压缩数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Compress(byte[] data)
{
byte[] bData;
MemoryStream ms = new MemoryStream();
GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);
stream.Write(data, 0, data.Length);
stream.Close();
stream.Dispose();
//必须把stream流关闭才能返回ms流数据,不然数据会不完整
//并且解压缩方法stream.Read(buffer, 0, buffer.Length)时会返回0
bData = ms.ToArray();
ms.Close();
ms.Dispose();
return bData;
}
/// <summary>
/// 解压数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] data)
{
byte[] bData;
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
ms.Position = 0;
GZipStream stream = new GZipStream(ms, CompressionMode.Decompress, true);
byte[] buffer = new byte[1024];
MemoryStream temp = new MemoryStream();
int read = stream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
temp.Write(buffer, 0, read);
read = stream.Read(buffer, 0, buffer.Length);
}
//必须把stream流关闭才能返回ms流数据,不然数据会不完整
stream.Close();
stream.Dispose();
ms.Close();
ms.Dispose();
bData = temp.ToArray();
temp.Close();
temp.Dispose();
return bData;
}
}
}