using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using
System.Runtime.Serialization;
using
System.Runtime.Serialization.Formatters.Binary;
using
System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WCF_WinformCient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
{
//实例化WCF客户端
WS.DataTransfersClient client = new WS.DataTransfersClient( "BasicHttpBinding_IDataTransfers");
MemoryStream stream = new MemoryStream();
byte[] buffer;
//获取所用块压缩流,并组装
buffer = client.GetCurrentBuffer();
//while (client.ReadNextBuffer())
stream.Write(buffer,0,buffer.Length);
stream.Position = 0;
buffer = new byte[stream.Length];
stream.Read(buffer,0,buffer.Length);
stream.Close();
//解压压缩流
byte[] bytes = Compression(buffer, CompressionMode.Decompress);
stream = new MemoryStream(bytes);
IFormatter formatter = new BinaryFormatter();
//反序列化
DataSet ds = (DataSet)formatter.Deserialize(stream);
stream.Close();
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "test";
this.label1.Text = ds.Tables[0].Rows.Count.ToString();
client.Close();
}
private byte[] Compression( byte[] data, CompressionMode mode)
{
DeflateStream zip = null;
try
{
if (mode == CompressionMode.Compress)
{
MemoryStream ms = new MemoryStream();
zip = new DeflateStream(ms, mode, true);
zip.Write(data, 0, data.Length);
zip.Close();
return ms.ToArray();
}
else
{
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
ms.Flush();
ms.Position = 0;
zip = new DeflateStream(ms, mode, true);
MemoryStream os = new MemoryStream();
int SIZE = 1024;
byte[] buf = new byte[SIZE];
int l = 0;
do
{
l = zip.Read(buf, 0, SIZE);
if (l == 0) l = zip.Read(buf, 0, SIZE);
os.Write(buf, 0, l);
} while (l != 0);
zip.Close();
return os.ToArray();
}
}
catch
{
if (zip != null) zip.Close();
return null;
}
finally
{
if (zip != null) zip.Close();
}
}
}
}