123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- namespace LYFZ.WinAPI
- {
- /// <summary>
- /// DataSet对象序列化并压缩和解压缩及反序列化
- /// </summary>
- public class DataSetSerializerDeserialize
- {
- public DataSetSerializerDeserialize() {
-
- }
- #region 2017-02-22 重新整理
- /// <summary>
- /// 不压缩直接序列化object到byte[]
- /// </summary>
- /// <param name="ds"></param>
- public static byte[] ObjectSerializerRetBytes(object obj)
- {
- if (obj != null)
- {
- IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
- using (MemoryStream ms = new MemoryStream())//创建内存流对象
- {
- formatter.Serialize(ms, obj);//把DataSet对象序列化到内存流
- byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
- ms.Close();//关闭内存流对象
- ms.Dispose();//释放资源
- return buffer;
- }
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 反序列化未压缩的object
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static object ObjectDeserialize(byte[] data)
- {
- if (data != null)
- {
- using (MemoryStream gzipms = new MemoryStream())//创建内存流对象
- {
- gzipms.Write(data, 0, data.Length);
- gzipms.Position = 0;
- BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
- try
- {
- if (gzipms.Length > 0)
- {
- return sfFormatter.Deserialize(gzipms);//反序列化
- }
- else
- {
- return null;
- }
- }
- catch
- {
- throw;
- }
- finally
- {
- gzipms.Close();//关闭内存流
- gzipms.Dispose();//释放资源
- }
- }
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 序列化object对象并压缩到byte[]
- /// </summary>
- /// <param name="ds"></param>
- public static byte[] ObjectSerializerCompressionRetBytes(object obj)
- {
- IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
- using (MemoryStream ms = new MemoryStream(), gzipms = new MemoryStream())//创建内存流对象
- {
- formatter.Serialize(ms, obj);//把DataSet对象序列化到内存流
- byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
- ms.Close();//关闭内存流对象
- ms.Dispose();//释放资源
- // MemoryStream gzipms = new MemoryStream();//创建内存流对象
- using (GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Compress, true))//创建压缩对象
- {
- gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件
- gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整
- gzipStream.Dispose();//释放对象
- }
- gzipms.Position = 0;
- byte[] retbuffer = gzipms.ToArray();
- gzipms.Close();
- gzipms.Dispose();
- return retbuffer;
- }
- }
- /// <summary>
- /// 反序列化压缩的Object
- /// </summary>
- /// <param name="_filePath"></param>
- /// <returns></returns>
- public static object ObjectDeserializeDecompress(byte[] data)
- {
- using (MemoryStream gzipms = new MemoryStream(), decompressms = new MemoryStream())//创建内存流对象
- {
- gzipms.Write(data, 0, data.Length);
- gzipms.Position = 0;
- using (GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Decompress))//创建解压对象
- {
- byte[] buffer = new byte[4096];
- int offset = 0;//定义读取位置
- // MemoryStream decompressms = new MemoryStream();//定义解压的内存流
- try
- {
- while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- decompressms.Write(buffer, 0, offset);//解压后的数据写入内存流
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- finally
- {
- gzipms.Close();
- gzipms.Dispose();
- gzipStream.Close();//关闭解压缩流
- gzipStream.Dispose();//释放资源
- }
- }
- decompressms.Position = 0;
- decompressms.Seek(0, SeekOrigin.Begin);
- BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
- try
- {
- return sfFormatter.Deserialize(decompressms);//反序列化
- }
- catch
- {
- throw;
- }
- finally
- {
- decompressms.Close();//关闭内存流
- decompressms.Dispose();//释放资源
- }
- }
- }
- #endregion
- /// <summary>
- /// 序列化DataSet对象并压缩
- /// </summary>
- /// <param name="ds"></param>
- public static void DataSetSerializerCompression(DataSet ds)
- {
- IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
- MemoryStream ms = new MemoryStream();//创建内存流对象
- formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流
- byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
- ms.Close();//关闭内存流对象
- ms.Dispose();//释放资源
- FileStream fs = File.Create("datasetCompression.dat");//创建文件
- GZipStream gzipStream = new GZipStream(fs, CompressionMode.Compress, true);//创建压缩对象
-
- gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件
- gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整
- gzipStream.Dispose();//释放对象
- fs.Close();//关闭流
- fs.Dispose();//释放对象
- }
- /// <summary>
- /// 把byte[]压缩并返回压缩后的byte[]
- /// </summary>
- /// <param name="dataArray"></param>
- public static byte[] DataCompressionRetBytes(byte[] dataArray)
- {
- try
- {
- MemoryStream gzipms = new MemoryStream();//创建内存流对象
- GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Compress, true);//创建压缩对象
- gzipStream.Write(dataArray, 0, dataArray.Length);//把压缩后的数据写入文件
- gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整
- byte[] retDataArray = gzipms.ToArray();
- gzipStream.Dispose();//释放对象
- gzipms.Position = 0;
- return gzipms.ToArray();
- }
- catch {
- return new byte[0];
- }
- }
- /// <summary>
- /// 解压缩并返回解压缩后的byte[]
- /// </summary>
- /// <param name="dataArray"></param>
- /// <returns></returns>
- public static byte[] DataDecompressRetBytes(byte[] dataArray)
- {
- try
- {
- MemoryStream gzipms = new MemoryStream();//创建内存流对象
- gzipms.Write(dataArray, 0, dataArray.Length);
- gzipms.Position = 0;
- GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Decompress);//创建解压对象
- byte[] buffer = new byte[4096];
- int offset = 0;//定义读取位置
- MemoryStream decompressms = new MemoryStream();//定义解压的内存流
- while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- decompressms.Write(buffer, 0, offset);//解压后的数据写入内存流
- }
- gzipms.Close();
- gzipms.Dispose();
- decompressms.Position = 0;
- byte[] retDataArray = decompressms.ToArray();
- decompressms.Close();//关闭内存流
- decompressms.Dispose();//释放资源
- gzipStream.Close();//关闭解压缩流
- gzipStream.Dispose();//释放资源
- return retDataArray;
- }
- catch
- {
- return new byte[0];
- }
- }
- /// <summary>
- /// 序列化DataSet对象并压缩到byte[]
- /// </summary>
- /// <param name="ds"></param>
- public static byte[] DataSetSerializerCompressionRetBytes(DataSet ds)
- {
- IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
- MemoryStream ms = new MemoryStream();//创建内存流对象
- formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流
- byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
- ds.Clear();
- ds.Dispose();
- ds = null;
- ms.Close();//关闭内存流对象
- ms.Dispose();//释放资源
- MemoryStream gzipms = new MemoryStream();//创建内存流对象
- GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Compress, true);//创建压缩对象
- gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件
- gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整
- gzipStream.Dispose();//释放对象
- gzipms.Position = 0;
- byte[] retbuffer = gzipms.ToArray();
- gzipms.Close();
- gzipms.Dispose();
- return retbuffer;
- }
- /// <summary>
- /// 不压缩直接序列化DataSet到byte[]
- /// </summary>
- /// <param name="ds"></param>
- public static byte[] DataSetSerializerRetBytes(DataSet ds)
- {
- IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
- MemoryStream ms = new MemoryStream();//创建内存流对象
- formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流
- byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
- ms.Close();//关闭内存流对象
- ms.Dispose();//释放资源
- ds.Clear();
- ds.Dispose();
- ds = null;
- return buffer;
- }
- /// <summary>
- /// 不压缩直接序列化DataSet
- /// </summary>
- /// <param name="ds"></param>
- public static void DataSetSerializer(DataSet ds)
- {
- IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
- FileStream fs = File.Create("dataset.dat");//创建文件
- formatter.Serialize(fs, ds);//把DataSet对象序列化到文件
- fs.Close();//关闭流
- fs.Dispose();//释放对象
- }
- /// <summary>
- /// 反序列化压缩的DataSet
- /// </summary>
- /// <param name="_filePath"></param>
- /// <returns></returns>
- public static DataSet DataSetDeserializeDecompress(byte[] data)
- {
- MemoryStream gzipms = new MemoryStream();//创建内存流对象
- gzipms.Write(data, 0, data.Length);
- gzipms.Position = 0;
- GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Decompress);//创建解压对象
- byte[] buffer = new byte[4096];
- int offset = 0;//定义读取位置
- MemoryStream decompressms = new MemoryStream();//定义解压的内存流
- try
- {
- while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- decompressms.Write(buffer, 0, offset);//解压后的数据写入内存流
- }
-
- }
- catch (Exception e)
- {
- throw e;
- }
- finally {
- gzipms.Close();
- gzipms.Dispose();
- }
- decompressms.Position = 0;
- decompressms.Seek(0, SeekOrigin.Begin);
- BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
- DataSet ds;
- try
- {
- ds = (DataSet)sfFormatter.Deserialize(decompressms);//反序列化
- }
- catch
- {
- throw;
- }
- finally
- {
- decompressms.Close();//关闭内存流
- decompressms.Dispose();//释放资源
- }
- gzipStream.Close();//关闭解压缩流
- gzipStream.Dispose();//释放资源
- return ds;
- }
- /// <summary>
- /// 反序列化压缩的DataSet
- /// </summary>
- /// <param name="_filePath"></param>
- /// <returns></returns>
- public static DataSet DataSetDeserializeDecompress(string _filePath)
- {
- FileStream fs = File.OpenRead(_filePath);//打开文件
- fs.Position = 0;//设置文件流的位置
- GZipStream gzipStream = new GZipStream(fs, CompressionMode.Decompress);//创建解压对象
- byte[] buffer = new byte[4096];//定义数据缓冲
- int offset = 0;//定义读取位置
- MemoryStream ms = new MemoryStream();//定义内存流
- while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- ms.Write(buffer, 0, offset);//解压后的数据写入内存流
- }
- BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
- ms.Position = 0;//设置内存流的位置
- DataSet ds;
- try
- {
- ds = (DataSet)sfFormatter.Deserialize(ms);//反序列化
- }
- catch
- {
- throw;
- }
- finally
- {
- ms.Close();//关闭内存流
- ms.Dispose();//释放资源
- }
- fs.Close();//关闭文件流
- fs.Dispose();//释放资源
- gzipStream.Close();//关闭解压缩流
- gzipStream.Dispose();//释放资源
- return ds;
- }
- /// <summary>
- /// 反序列化未压缩的DataSet
- /// </summary>
- /// <param name="_filePath"></param>
- /// <returns></returns>
- public static DataSet DataSetDeserialize(byte[] data)
- {
- MemoryStream gzipms = new MemoryStream();//创建内存流对象
- gzipms.Write(data, 0, data.Length);
- gzipms.Position = 0;
- BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
- DataSet ds;
- try
- {
- ds = (DataSet)sfFormatter.Deserialize(gzipms);//反序列化
- }
- catch
- {
- throw;
- }
- finally
- {
- gzipms.Close();//关闭内存流
- gzipms.Dispose();//释放资源
- }
- return ds;
- }
- /// <summary>
- /// 反序列化未压缩的DataSet
- /// </summary>
- /// <param name="_filePath"></param>
- /// <returns></returns>
- public static DataSet DataSetDeserialize(string _filePath)
- {
- FileStream fs = File.OpenRead(_filePath);//打开文件
- fs.Position = 0;//设置文件流的位置
- BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
- DataSet ds;
- try
- {
- ds = (DataSet)sfFormatter.Deserialize(fs);//反序列化
- }
- catch
- {
- throw;
- }
- finally
- {
- fs.Close();//关闭内存流
- fs.Dispose();//释放资源
- }
- fs.Close();//关闭文件流
- fs.Dispose();//释放资源
- return ds;
- }
- /* static void Main(string[] args)
- {
- DataSet dataSet = new DataSet();
- DataSetSerializer(dataSet);
- DataSetSerializerCompression(dataSet);
- } */
- }
- }
|