由于公司近期业务的需要,本要研究一些有关 XML WebServer下数据序列化及数据压缩的实现问题,现与大家分享一下,也希望大家勇于讨论,互相学习。由于要实现网络传输入,考虑到操作系统、网络性能等几方面的原因,就需要对数据实现序列化和数据压缩传输入。本人简单地实验了一下,如果在XML WebServer下直接使用DataSet传输入数据,就算实现压缩,也是非常具大的。最好的方法是自己定义好结构体或都类对象(当然在定义的时间要声明我可序列化的哟),进行序列化,然后再进行数据压缩;到了另外一端时选进行解压缩,然后再反序列化。通过这样的实现,从我的项目经验得到的结论是,大大地提高了数据传输的性能,尤其是对于数据量具大的数据传输入更是如此。本人已把数据对象序列化和反序列化、压缩和解压方法(引用的是开源插件,可以从网上http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 下载)封装成一个完整的类中了,代码如文中所述,如有不懂不解之处希望各位一起讨论,也希望能听到更好的见解。
/**/ /********************************************************************************************* * 文 件 名:ObjectBinaryFormate.cs * 创 建 人:王峰 * 功能说明:实现对象序列化与反序列 * 创建日期:2006-12-25 * 单位名称:华域科技 * 版 本:1.0 * 更新说明:--------------------------------------------------------------------------------- * 修 改 人:王峰 * 日 期:2007-01-24 * 修改说明:实现数据的压缩 * ------------------------------------------------------------------------------------------- * 其它说明:无***********************************************************************************************/ using System; using System.IO; using System.Data; using System.Runtime.Serialization.Formatters.Binary; // 此处需要进相关的网站下载相应的开源插件 using ICSharpCode.SharpZipLib.Zip.Compression; namespace SmsClientApp ... { /**//// <summary> /// ObjectBinaryFormate 的摘要说明。 /// </summary> public class ObjectBinaryFormate ...{ public ObjectBinaryFormate() ...{ } /**//********************************************************************************************** * 方法名称:ChangeObjectToBytes * 功能说明:把数据对象序列化为字节型数组 * 输 入 值:数据对象 * 输 出 值:无 * 返 回 值:字节数组 * 其它说明:无 **********************************************************************************************/ public static byte[] ChangeObjectToBytes(object objValue) ...{ byte[] dataValue = null; try ...{ //序列化 BinaryFormatter formate = new BinaryFormatter(); //内存文件流对象 MemoryStream smsStream = new MemoryStream(); formate.Serialize(smsStream, objValue); dataValue = smsStream.ToArray(); smsStream.Close(); } catch(Exception e) ...{ System.Console.WriteLine("序列化失败!"+e.Message); } //返回压缩后的数据 return CompressByteData(dataValue); } /**//********************************************************************************************** * 方法名称:ChangeBytesToObject * 功能说明:把字节型数组反序列为数据对象 * 输 入 值:字节数组 * 输 出 值:无 * 返 回 值:数据对象 * 其它说明:无 **********************************************************************************************/ public static object ChangeBytesToObject(byte[] dataValue) ...{ object objValue = null; try ...{ //解压缩数据 byte[] resultValue = DecompressByteData(dataValue); //反序列化 BinaryFormatter formate = new BinaryFormatter(); //内存文件流对象 MemoryStream smsStream = new MemoryStream(); smsStream.Write(resultValue, 0, resultValue.Length); //指针归零 smsStream.Seek(0, SeekOrigin.Begin); objValue = (object)formate.Deserialize(smsStream); smsStream.Close(); } catch(Exception e) ...{ System.Console.WriteLine("反序列化失败!"+e.Message); } //返回对象 return objValue; } /**//********************************************************************************************** * 方法名称:CompressByteData * 功能说明:数据压缩 * 输 入 值:源字节数据 * 输 出 值:无 * 返 回 值:压缩后的字节数据 * 其它说明:无 **********************************************************************************************/ private static byte[] CompressByteData(Byte[] dataValue) ...{ byte[] resultValue = null; try ...{ //压缩数据 Deflater compressFile = new Deflater(Deflater.BEST_COMPRESSION); compressFile.SetInput(dataValue); compressFile.Finish(); //内存文件流对象 MemoryStream smsStream = new MemoryStream(); byte[] bufData = new byte[1024]; while (!compressFile.IsFinished) ...{ int bufLength = compressFile.Deflate(bufData); smsStream.Write(bufData, 0, bufLength); } resultValue = smsStream.ToArray(); smsStream.Close(); } catch(Exception e) ...{ System.Console.WriteLine("压缩数据失败!"+e.Message); } //返回压缩后的数据 return resultValue; } /**//********************************************************************************************** * 方法名称:DecompressByteData * 功能说明:解压缩数据 * 输 入 值:源字节数据 * 输 出 值:无 * 返 回 值:解压后数据 * 其它说明:无 **********************************************************************************************/ private static byte[] DecompressByteData(byte[] dataValue) ...{ byte[] resultValue = null; try ...{ //解压缩数据 Inflater decompressFile = new Inflater(); decompressFile.SetInput(dataValue); //内存文件流对象 MemoryStream smsStream = new MemoryStream(); byte[] bufData = new byte[1024]; while (!decompressFile.IsFinished) ...{ int bufLength = decompressFile.Inflate(bufData); smsStream.Write(bufData, 0, bufLength); } resultValue = smsStream.ToArray(); smsStream.Close(); } catch(Exception e) ...{ System.Console.WriteLine("解压缩数据失败!"+e.Message); } //返回数据 return resultValue; } }}