.net的序列化與反序列化實例。本站提示廣大學習愛好者:(.net的序列化與反序列化實例)文章只能為提供參考,不一定能成為您想要的結果。以下是.net的序列化與反序列化實例正文
本文實例講述了.net的序列化與反序列化的完成辦法。分享給年夜家供年夜家參考。詳細辦法以下:
1.序列化與反序列化概述
C#中假如須要:將一個構造很龐雜的類的對象存儲起來,或許經由過程網路傳輸到長途的客戶端法式中去,這時候就須要用到序列化,反序列化(Serialization & Deserialization)
2.BinaryFormattter
.NET中串行有三種,BinaryFormatter, SoapFormatter和XmlSerializer.
個中BinaryFormattter最簡略,它是直接用二進制方法把對象 (Object)停止串行或反串,他的長處是速度快,可以串行private或許protected的member, 在分歧版本的。NET中都兼容,可以看做是。NET本身的本命辦法,固然缺陷也就隨之而來了,分開了。NET它就活不了,所以不克不及在其他平台或跨網路長進 行。
3.序列化
BinaryFormatter ser = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, DS);
byte[] buffer = ms.ToArray();
MemoryStream :創立其支撐存儲區為內存的流
4.反序列化
//反序列化:將byte[]型的數據,放到Stream中,BinaryFormatter將流中的數據反序列化成對象
MemoryStream ms = new MemoryStream(bytes);
BinaryFormatter ser = new BinaryFormatter();
DataSetSurrogate dss = ser.Deserialize(ms) asDataSetSurrogate;
5.完全實例:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Compression;
using System.IO;
namespace Common
{
/// <summary>
/// 應用GzipStream停止緊縮息爭壓
/// </summary>
public class GZipUtil
{
private static GZipStream gZipStream = null;
/// <summary>
/// 緊縮
/// </summary>
/// <param name="srcBytes"></param>
/// <returns></returns>
public static byte[] Compress(byte[] srcBytes)
{
MemoryStream ms = new MemoryStream(srcBytes);
gZipStream = new GZipStream(ms, CompressionMode.Compress);
gZipStream.Write(srcBytes, 0, srcBytes.Length);
gZipStream.Close();
return ms.ToArray();
}
/// <summary>
/// 解壓
/// </summary>
/// <param name="srcBytes"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] srcBytes)
{
MemoryStream srcMs = new MemoryStream(srcBytes);
gZipStream = new GZipStream(srcMs, CompressionMode.Decompress);
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[40960];
int n;
while ((n = gZipStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, n);
}
gZipStream.Close();
return ms.ToArray();
}
/// <summary>
/// 將指定的字節數組緊縮,並寫入到目的文件
/// </summary>
/// <param name="srcBuffer">指定的源字節數組</param>
/// <param name="destFile">指定的目的文件</param>
public static void CompressData(byte[] srcBuffer, string destFile)
{
FileStream destStream = null;
GZipStream compressedStream = null;
try
{
//翻開文件流
destStream = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write);
//指定緊縮的目標流(這裡是文件流)
compressedStream = new GZipStream(destStream, CompressionMode.Compress, true);
//往目標流中寫數據,而流將數據寫到指定的文件
compressedStream.Write(srcBuffer, 0, srcBuffer.Length);
}
catch (Exception ex)
{
throw new Exception(String.Format("緊縮數據寫入文件{0}時產生毛病", destFile), ex);
}
finally
{
// Make sure we allways close all streams
if (null != compressedStream)
{
compressedStream.Close();
compressedStream.Dispose();
}
if (null != destStream)
destStream.Close();
}
}
/// <summary>
/// 將指定的文件解壓,前往解壓後的數據
/// </summary>
/// <param name="srcFile">指定的源文件</param>
/// <returns>解壓後獲得的數據</returns>
public static byte[] DecompressData(string srcFile)
{
if (false == File.Exists(srcFile))
throw new FileNotFoundException(String.Format("找不到指定的文件{0}", srcFile));
FileStream sourceStream = null;
GZipStream decompressedStream = null;
byte[] quartetBuffer = null;
try
{
sourceStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);
// Read the footer to determine the length of the destiantion file
//GZIP文件格局解釋:
//10字節的頭,包括幻數、版本號和時光戳
//可選的擴大頭,如原文件名
//文件體,包含DEFLATE緊縮的數據
//8字節的尾注,包含CRC-32校驗和和未緊縮的原始數據長度(4字節) 文件年夜小不跨越4G
//為Data指定byte的長度,有意開年夜byte數據的規模
//讀取未緊縮的原始數據長度
quartetBuffer = new byte[4];
long position = sourceStream.Length - 4;
sourceStream.Position = position;
sourceStream.Read(quartetBuffer, 0, 4);
int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
byte[] data;
if (checkLength <= sourceStream.Length)
{
data = new byte[Int16.MaxValue];
}
else
{
data = new byte[checkLength + 100];
}
//每100byte從解壓流中讀出數據,並將讀出的數據Copy到Data byte[]中,如許就完成了對數據的解壓
byte[] buffer = new byte[100];
sourceStream.Position = 0;
int offset = 0;
int total = 0;
while (true)
{
int bytesRead = decompressedStream.Read(buffer, 0, 100);
if (bytesRead == 0)
break;
buffer.CopyTo(data, offset);
offset += bytesRead;
total += bytesRead;
}
//剔除過剩的byte
byte[] actualdata = new byte[total];
for (int i = 0; i < total; i++)
actualdata[i] = data[i];
return actualdata;
}
catch (Exception ex)
{
throw new Exception(String.Format("從文件{0}解壓數據時產生毛病", srcFile), ex);
}
finally
{
if (sourceStream != null)
sourceStream.Close();
if (decompressedStream != null)
decompressedStream.Close();
}
}
}
}
6.小結
停止序列化,反序列化,應用到的都是BinaryFormate,都得借通俗流MemoryStream,分歧的是:
序列化時,將對象序列化後放到MemoryStream,而反序列化時,將MemoryStream中的byte[]數據,反序列成對象
願望本文所述對年夜家的C#法式設計有所贊助。