首页 > 代码库 > web Service dataset压缩传输
web Service dataset压缩传输
服务端:
using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using System.IO.Compression;using System.Data;
using CompressDataSet;[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。// [System.Web.Script.Services.ScriptService]public class Service : System.Web.Services.WebService{ public klmywgSoapHeader header = new klmywgSoapHeader(); public Service () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] [SoapHeader("header")] public string HelloWorld() { string result= header.Validate(); if (result == "true") { return "Hello World"; } else return result; } private DataSet GetDataSet() { DataSet ds = new DataSet(); DataTable dt = new DataTable(); ds.Tables.Add(dt); dt.Columns.Add("UserName", typeof(string)); DataRow dr = dt.NewRow(); dr["UserName"] = "UserName"; dt.Rows.Add(dr); return ds; } [WebMethod(Description = "直接返回DataSetSurrogate对象,并用Binary序列化后并且ZIP压缩的字节数组")] public byte[] GetDataSetSurrogateZipBytes() { DataSet ds = GetDataSet(); DataSetSurrogate dss = new DataSetSurrogate(ds); BinaryFormatter ser = new BinaryFormatter(); //序列化对象 MemoryStream ms = new MemoryStream(); //内存流 ser.Serialize(ms, dss); byte[] buffer = ms.ToArray(); //字节流 byte[] bufferZip = ComPress(buffer); return bufferZip ; }
//压缩方法
public byte[] ComPress(byte[] data) { try { MemoryStream 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 = http://www.mamicode.com/new byte[ms.Length]; ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString())); return compressed_data; } catch { return null; } } }
客户端:
using System.IO;
using System.IO.Compression;
byte[] zipBuffer = service.GetDataSetSurrogateZipBytes(); byte[] buffer = UnZipClass.Decompress(zipBuffer); BinaryFormatter ser = new BinaryFormatter(); DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet dataSet = dss.ConvertToDataSet();
解压:UnZipClass.cs
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.IO;using System.IO.Compression;using System.Runtime.Serialization.Formatters.Binary;public static class UnZipClass{ public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); Stream zipStream = null; zipStream = new GZipStream(ms, CompressionMode.Decompress); byte[] dc_data = http://www.mamicode.com/null; dc_data = ExtractBytesFromStream(zipStream, data.Length); return dc_data; } catch { return null; } } public static byte[] ExtractBytesFromStream(Stream zipStream, int dataBlock) { byte[] data = http://www.mamicode.com/null; int totalBytesRead = 0; try { while (true) { Array.Resize(ref data, totalBytesRead + dataBlock + 1); int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock); if (bytesRead == 0) { break; } totalBytesRead += bytesRead; } Array.Resize(ref data, totalBytesRead); return data; } catch { return null; } }}
web Service dataset压缩传输
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。