站长中国 - 站长社区 - 论坛列表 - C#
登陆 注册
1
[引用] 主题:C# DataSet对象解压缩及反序列化
  • loveu
  • 等级:初学乍练
  • 积分:10
  • 帖子:0
  • 精华:0

昨天说了DataSet的序列化及压缩,今天把解压缩及反序列化的代码写一下:


 


view plaincopy to clipboardprint?
/// <summary>  
/// 反序列化压缩的DataSet  
/// </summary>  
/// <param name="_filePath"></param>  
/// <returns></returns>  
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>  
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;  


http://blog.csdn.net/wlkjhxd/archive/2009/03/25/4022544.aspx


快速回复
标题: 可不填写
内容:
[b] 加粗 [/b] = 加粗
表情:   grin  LOL  cheese  smile  wink  smirk  rolleyes  confused  surprised  big surprise  tongue laugh  tongue rolleye  tongue wink  rasberry  blank stare  long face  ohh  grrr  gulp
  downer  red face  sick  shut eye  hmmm  mad  angry  zipper  kiss  shock  cool smile  cool smirk  cool grin  cool hmm  cool mad  cool cheese  vampire  snake  excaim

热门帖子

精华帖子

最新帖子

© CopyRight 2006-2009,CNZZAD.COM, Inc. All Rights Reserved M: 2.27MB T: 0.1028
鲁ICP备05001756号
站长中国群1:30492658 精致合租咨询群1:21445564