Dataset的序列化传输,压缩和反序列化
using System;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
namespace TestTransfers
{
private void InitBuffers(DataSet ds)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream stream_ = new MemoryStream();
formatter.Serialize(stream_, ds);
buffer_all = stream_.ToArray();
stream_.Close();
byte[] bytes_c = Compression(buffer_all, CompressionMode.Compress);//Compress
stream = new MemoryStream(bytes_c);
stream.Position = 0;
remain_length = stream.Length;
}
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();
}
}
private void Deserialize()
{
MemoryStream stream = new MemoryStream();
byte[] buffer;
while(client.ReadNextBuffer())
{
buffer = client.GetCurrentBuffer();
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();
}
}
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
namespace TestTransfers
{
private void InitBuffers(DataSet ds)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream stream_ = new MemoryStream();
formatter.Serialize(stream_, ds);
buffer_all = stream_.ToArray();
stream_.Close();
byte[] bytes_c = Compression(buffer_all, CompressionMode.Compress);//Compress
stream = new MemoryStream(bytes_c);
stream.Position = 0;
remain_length = stream.Length;
}
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();
}
}
private void Deserialize()
{
MemoryStream stream = new MemoryStream();
byte[] buffer;
while(client.ReadNextBuffer())
{
buffer = client.GetCurrentBuffer();
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();
}
}