将DataSet保存为磁盘文件,所用到的技术点序列化DS,反序列化,压缩,减压缩
2011-11-15 16:36
405 查看
一个序列化DataSet对象类,主要功能:将DataSet保存为磁盘文件,所用到的技术点序列化DS,反序列化,压缩,减压缩
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.Data; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.IO.Compression; namespace Vancl.TMS.Sync { public class SetSerializer { /// <summary> /// 序列化DataSet对象并压缩 /// </summary> /// <param name="ds"></param> public static string DataSetSerializerCompression(DataSet ds,string filename) { string fileName = filename; try { IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象 MemoryStream ms = new MemoryStream();//创建内存流对象 formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流 byte[] buffer = ms.ToArray();//把内存流对象写入字节数组 ms.Close();//关闭内存流对象 ms.Dispose();//释放资源 if (File.Exists(filename)) { File.Delete(filename); } FileStream fs = File.Create(fileName);//创建文件 GZipStream gzipStream = new GZipStream(fs, CompressionMode.Compress, true);//创建压缩对象 gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件 gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整 gzipStream.Dispose();//释放对象 fs.Close();//关闭流 fs.Dispose();//释放对象 } catch (Exception ex) { fileName = string.Empty; //写日志 throw ex; } return fileName; } /// <summary> /// 不压缩直接序列化DataSet /// </summary> /// <param name="ds"></param> public static string DataSetSerializer(DataSet ds, string fileName) { try { IFormatter formatter = new BinaryFormatter(); //定义BinaryFormatter以序列化DataSet对象 FileStream fs = File.Create(fileName); //创建文件 formatter.Serialize(fs, ds); //把DataSet对象序列化到文件 fs.Close(); //关闭流 fs.Dispose(); //释放对象 } catch (Exception) { fileName = string.Empty; //写日志 throw; } return fileName; } /// <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(string fileName) { FileStream fs = File.OpenRead(fileName);//打开文件 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; } } }
相关文章推荐
- WebService中Dataset的压缩序列化和解压反序列化
- WebService中Dataset的压缩序列化和解压反序列化(DataSetSurrogate的使用)
- WebService中Dataset的压缩序列化和解压反序列化(DataSetSurrogate的使用)
- WebService中Dataset的压缩序列化和解压反序列化 >> DataSetSurrogate的使用
- WebService使用DataSetSurrogate压缩Dataset的序列化和解压反序列化传输
- WebService使用DataSetSurrogate压缩Dataset的序列化和解压反序列化传输 (转)
- WebService中Dataset的压缩序列化和解压反序列化(DataSetSurrogate的使用)
- WebService使用DataSetSurrogate压缩Dataset的序列化和解压反序列化传输
- C# DataSet对象序列化并压缩及反序列化
- C# DataSet对象序列化并压缩
- c# 实现类的序列化与反序列化 可保存于session viewstate cookie各种缓存中
- DataSet压缩,序列化
- 序列化、压缩、解压缩、反序列化对象
- 决策树的保存——序列化和反序列化
- 序列化和反序列化技术
- Redis+protostuff 实现对象序列化保存,反序列化获取
- 序列化和反序列化技术
- DataTable的序列化和反序列化(格式不变保存文件和读取)
- 我记录网站综合系统 -- 技术原理解析[5:JSON 序列化 反序列化]
- 一起谈.NET技术,ASP.NET 中JSON 的序列化和反序列化