using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Text;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
namespace CommonClass
{
/// <summary>
/// 功 能:對資料的基本操作
public class KCDataFormatter
{
public KCDataFormatter() { }
/// <summary>
/// 将DataSet格式化成位元組數組byte[]
/// </summary>
/// <param name="dsOriginal">DataSet對象</param>
/// <returns>位元組數組</returns>
public static byte[] GetBinaryFormatData(DataSet dsOriginal)
{
byte[] binaryDataResult = null;
MemoryStream memStream = new MemoryStream();
IFormatter brFormatter = new BinaryFormatter();
dsOriginal.RemotingFormat = SerializationFormat.Binary;
brFormatter.Serialize(memStream, dsOriginal);
binaryDataResult = memStream.ToArray();
memStream.Close();
memStream.Dispose();
return binaryDataResult;
}
/// 将DataSet格式化成位元組數組byte[],并且已經經過壓縮
public static byte[] GetBinaryFormatDataCompress(DataSet dsOriginal)
return Compress(binaryDataResult);
/// 解壓資料
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] data)
byte[] bData;
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
ms.Position = 0;
GZipStream stream = new GZipStream(ms, CompressionMode.Decompress, true);
byte[] buffer = new byte[1024];
MemoryStream temp = new MemoryStream();
int read = stream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
temp.Write(buffer, 0, read);
read = stream.Read(buffer, 0, buffer.Length);
}
//必須把stream流關閉才能傳回ms流資料,不然資料會不完整
stream.Close();
stream.Dispose();
ms.Close();
ms.Dispose();
bData = temp.ToArray();
temp.Close();
temp.Dispose();
return bData;
/// 壓縮資料
public static byte[] Compress(byte[] data)
GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);
stream.Write(data, 0, data.Length);
//并且解壓縮方法stream.Read(buffer, 0, buffer.Length)時會傳回0
bData = ms.ToArray();
/// 将位元組數組反序列化成DataSet對象
/// <param name="binaryData">位元組數組</param>
/// <returns>DataSet對象</returns>
public static DataSet RetrieveDataSet(byte[] binaryData)
DataSet ds = null;
MemoryStream memStream = new MemoryStream(binaryData,true);
//byte[] bs = memStream.GetBuffer();
// memStream.Write(bs, 0, bs.Length);
//memStream.Seek(0, SeekOrigin.Begin);
ds =(DataSet)brFormatter.Deserialize(memStream);
return ds;
/// 将位元組數組反解壓後序列化成DataSet對象
public static DataSet RetrieveDataSetDecompress(byte[] binaryData)
DataSet dsOriginal = null;
MemoryStream memStream = new MemoryStream(Decompress(binaryData));
Object obj = brFormatter.Deserialize(memStream);
dsOriginal = (DataSet)obj;
return dsOriginal;
/// 将object格式化成位元組數組byte[]
/// <param name="dsOriginal">object對象</param>
public static byte[] GetBinaryFormatData(object dsOriginal)
/// 将objec格式化成位元組數組byte[],并壓縮
public static byte[] GetBinaryFormatDataCompress(object dsOriginal)
/// 将位元組數組反序列化成object對象
/// <returns>object對象</returns>
public static object RetrieveObject(byte[] binaryData)
MemoryStream memStream = new MemoryStream(binaryData);
return obj;
/// 将位元組數組解壓後反序列化成object對象
public static object RetrieveObjectDecompress(byte[] binaryData)
///// <summary>
///// 解密配置檔案并讀入到xmldoc中
///// </summary>
//public static XmlNode DecryptConfigFile(string filePath)
//{
// FileStream fs = new FileStream(filePath, FileMode.Open);
// XmlDocument m_XmlDoc = new XmlDocument();
// BinaryFormatter formatter = null;
// try
// {
// formatter = new BinaryFormatter();
// // Deserialize the hashtable from the file and
// // assign the reference to the local variable.
// m_XmlDoc.LoadXml(KCEncrypt.Decrypt((string)formatter.Deserialize(fs)));
// return m_XmlDoc.DocumentElement;
// }
// catch (SerializationException e)
// Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
// throw;
// finally
// fs.Close();
// fs = null;
//}
///// 加密密鑰後再對檔案字元進行加密
//public static void EncryptConfigFile(string filePath, string str)
// FileStream fs = new FileStream(filePath, FileMode.Create);
// BinaryFormatter formatter = new BinaryFormatter();
// formatter.Serialize(fs, KCEncrypt.Encrypt(str));
// Console.WriteLine("Failed to serialize. Reason: " + e.Message);
}
}