.net的序列化與反序列化實(shí)例
本文實(shí)例講述了.net的序列化與反序列化的實(shí)現(xiàn)方法。分享給大家供大家參考。具體方法如下:
1.序列化與反序列化概述
C#中如果需要:將一個結(jié)構(gòu)很復(fù)雜的類的對象存儲起來,或者通過網(wǎng)路傳輸?shù)竭h(yuǎn)程的客戶端程序中去,這時(shí)就需要用到序列化,反序列化(Serialization & Deserialization)
2.BinaryFormattter
.NET中串行有三種,BinaryFormatter, SoapFormatter和XmlSerializer.
其中BinaryFormattter最簡單,它是直接用二進(jìn)制方式把對象 (Object)進(jìn)行串行或反串,他的優(yōu)點(diǎn)是速度快,可以串行private或者protected的member, 在不同版本的。NET中都兼容,可以看作是。NET自己的本命方法,當(dāng)然缺點(diǎn)也就隨之而來了,離開了。NET它就活不了,所以不能在其他平臺或跨網(wǎng)路上進(jìn) 行。
3.序列化
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, DS);
byte[] buffer = ms.ToArray();
MemoryStream :創(chuàng)建其支持存儲區(qū)為內(nèi)存的流
4.反序列化
MemoryStream ms = new MemoryStream(bytes);
BinaryFormatter ser = new BinaryFormatter();
DataSetSurrogate dss = ser.Deserialize(ms) asDataSetSurrogate;
5.完整實(shí)例:
using System.Collections.Generic;
using System.Text;
using System.IO.Compression;
using System.IO;
namespace Common
{
/// <summary>
/// 利用GzipStream進(jìn)行壓縮和解壓
/// </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>
/// 將指定的字節(jié)數(shù)組壓縮,并寫入到目標(biāo)文件
/// </summary>
/// <param name="srcBuffer">指定的源字節(jié)數(shù)組</param>
/// <param name="destFile">指定的目標(biāo)文件</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);
//往目的流中寫數(shù)據(jù),而流將數(shù)據(jù)寫到指定的文件
compressedStream.Write(srcBuffer, 0, srcBuffer.Length);
}
catch (Exception ex)
{
throw new Exception(String.Format("壓縮數(shù)據(jù)寫入文件{0}時(shí)發(fā)生錯誤", destFile), ex);
}
finally
{
// Make sure we allways close all streams
if (null != compressedStream)
{
compressedStream.Close();
compressedStream.Dispose();
}
if (null != destStream)
destStream.Close();
}
}
/// <summary>
/// 將指定的文件解壓,返回解壓后的數(shù)據(jù)
/// </summary>
/// <param name="srcFile">指定的源文件</param>
/// <returns>解壓后得到的數(shù)據(jù)</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字節(jié)的頭,包含幻數(shù)、版本號以及時(shí)間戳
//可選的擴(kuò)展頭,如原文件名
//文件體,包括DEFLATE壓縮的數(shù)據(jù)
//8字節(jié)的尾注,包括CRC-32校驗(yàn)和以及未壓縮的原始數(shù)據(jù)長度(4字節(jié)) 文件大小不超過4G
//為Data指定byte的長度,故意開大byte數(shù)據(jù)的范圍
//讀取未壓縮的原始數(shù)據(jù)長度
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從解壓流中讀出數(shù)據(jù),并將讀出的數(shù)據(jù)Copy到Data byte[]中,這樣就完成了對數(shù)據(jù)的解壓
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}解壓數(shù)據(jù)時(shí)發(fā)生錯誤", srcFile), ex);
}
finally
{
if (sourceStream != null)
sourceStream.Close();
if (decompressedStream != null)
decompressedStream.Close();
}
}
}
}
6.小結(jié)
進(jìn)行序列化,反序列化,利用到的都是BinaryFormate,都得借普通流MemoryStream,不同的是:
序列化時(shí),將對象序列化后放到MemoryStream,而反序列化時(shí),將MemoryStream中的byte[]數(shù)據(jù),反序列成對象
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#使用winform實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了C#使用winform實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
C#實(shí)現(xiàn)將Doc文檔轉(zhuǎn)換成rtf格式的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)將Doc文檔轉(zhuǎn)換成rtf格式的方法,結(jié)合實(shí)例形式分析了C#針對word文件的讀取及文檔格式轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
C#使用xsd文件驗(yàn)證XML格式是否正確的實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用xsd文件驗(yàn)證XML格式是否正確的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了C#針對xml文件的創(chuàng)建、驗(yàn)證相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果
這篇文章主要介紹了C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果,涉及C#針對窗口的參數(shù)設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
Unity UGUI的InputField輸入框組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的InputField輸入框組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

