1、 WebService 中 Dataset 的压缩序列化和解压反序列化分类: C#2012-04-09 00:09 178 人阅读 评论(0) 收藏 举报第一步:引入命名空间下载 DataSetSurrogate 组件using System.IO.Compression;using System.Runtime.Serialization.Formatters.Binary;将下载好的 DataSetSurrogate 放入 bin 路径下第二步:压缩序列化/序列化转化为二进制数组的 DataSetpublic byte GetDataSetSurrogateZipBYtes(DataSet
2、 DS)DataSetSurrogate dss = new DataSetSurrogate(DS);/二进制方式方式序列化BinaryFormatter ser = new BinaryFormatter();MemoryStream ms = new MemoryStream();ser.Serialize(ms, dss);byte buffer = ms.ToArray();/调用压缩方法byte Zipbuffer = Compress(buffer);return Zipbuffer;/压缩二进制文件private byte Compress(byte data)MemorySt
3、ream ms = new MemoryStream();Stream zipStream = null;zipStream = new GZipStream(ms, CompressionMode.Compress, true);/从指定的字节数组中将压缩的字节写入基础流zipStream.Write(data, 0, data.Length);zipStream.Close();ms.Position = 0;byte Compressed_Data = new bytems.Length;ms.Read(Compressed_Data, 0, int.Parse(ms.Length.To
4、String();return Compressed_Data;第三部:解压反序列化/将压缩后的二进制数组解压public static byte Decompress(byte data) /data 参数为压缩后的二进制数组tryMemoryStream ms = new MemoryStream(data);Stream zipStream = null;zipStream = new GZipStream(ms, CompressionMode.Decompress);byte dc_data = null;dc_data = EtractBytesFormStream(zipStre
5、am, data.Length);return dc_data;/返回解压后的二进制数组catchreturn null;/ 将二进制文件反序列化后转化为 DataSetpublic DataSet GetDatasetFromByte(byte ZipByte)byte buffer = UnZipClass.Decompress(ZipByte);BinaryFormatter ser = new BinaryFormatter();DataSetSurrogate dss;dss = (DataSetSurrogate)ser.Deserialize(new MemoryStream(buffer);DataSet DS = dss.ConvertToDataSet();return DS;