接下来就是要把数据进行压缩了,压缩前要进行数据的序列化,序列化代码如下:
/**//// <summary>
/// 序列化数据成byte[]
/// </summary>
/// <param name="o">未序列化的数据</param>
/// <returns>返回序列化后的byte[]数组</returns>
public static byte[] byteXmlSerializer(object o)
{
if (o == null)
{
throw new ArgumentNullException("null input");
}
try
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(o.GetType());
System.IO.MemoryStream mem = new MemoryStream();
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(mem, Encoding.Default);
ser.Serialize(writer, o);
writer.Close();
return mem.ToArray();
}
catch (Exception ex)
{
throw ex;
}
}
/**//// <summary>
/// 反序列化数据
/// </summary>
/// <param name="input">序列化后的数据</param>
/// <param name="type">被序列化的数据类型</param>
/// <returns>返回反序列化后的object数据</returns>
public static object objXmlDeserialize(byte[] input, Type type)
{
if (input == null)
{
throw new ArgumentNullException("null input");
}
try
{
XmlSerializer mySerializer = new XmlSerializer(type);
StreamReader stmRead = new StreamReader(new MemoryStream(input), System.Text.Encoding.Default);
return mySerializer.Deserialize(stmRead);
}
catch (Exception ex)
{
throw ex;
}
}
/// 序列化数据成byte[]
/// </summary>
/// <param name="o">未序列化的数据</param>
/// <returns>返回序列化后的byte[]数组</returns>
public static byte[] byteXmlSerializer(object o)
{
if (o == null)
{
throw new ArgumentNullException("null input");
}
try
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(o.GetType());
System.IO.MemoryStream mem = new MemoryStream();
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(mem, Encoding.Default);
ser.Serialize(writer, o);
writer.Close();
return mem.ToArray();
}
catch (Exception ex)
{
throw ex;
}
}
/**//// <summary>
/// 反序列化数据
/// </summary>
/// <param name="input">序列化后的数据</param>
/// <param name="type">被序列化的数据类型</param>
/// <returns>返回反序列化后的object数据</returns>
public static object objXmlDeserialize(byte[] input, Type type)
{
if (input == null)
{
throw new ArgumentNullException("null input");
}
try
{
XmlSerializer mySerializer = new XmlSerializer(type);
StreamReader stmRead = new StreamReader(new MemoryStream(input), System.Text.Encoding.Default);
return mySerializer.Deserialize(stmRead);
}
catch (Exception ex)
{
throw ex;
}
}