首页 新闻 会员 周边 捐助

关于对象序列化压缩后追加,再反序列化时丢失对象的问题

0
悬赏园豆:50 [待解决问题]

实现描述: 先添加10个对象,然后序列化并压缩,保持到文件,然后在添加10个对象,然后序列化并压缩,并

追加到前面保持的文件中,然后读取文件解压缩,反序列化,得到一共添加的20个对象

[color=#FF0000]问题:解压缩的时候,只解压出来前10个对象的数据流,后10个对象的数据流没有解压,所以

只能反序列化出前10个对象,后10个丢失[/color]

代码如下:

        /// <summary>       
        /// 序列化对象并压缩       
        /// </summary>       
        /// <param name="ds"></param>     
        static void DataSetSerializerCompression(List<Person> list, string savePath, string

saveName)
        {
            IFormatter formatter = new BinaryFormatter();   
            MemoryStream ms = new MemoryStream();
            formatter.Serialize(ms, list);   
         
            byte[] buffer = ms.ToArray(); 
            ms.Close();
            ms.Dispose();
            if (!Directory.Exists(savePath)) 
                Directory.CreateDirectory(savePath);
            FileStream fs = File.Create(savePath + saveName);
            GZipStream gzipStream = new GZipStream(fs, CompressionMode.Compress, true);   
            gzipStream.Write(buffer, 0, buffer.Length);    
            gzipStream.Close();   
            gzipStream.Dispose();   
            fs.Close();    
            fs.Dispose();   
        }


        /// 序列化对象的追加
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="ds"></param>
        static void DataSerializerAppend(string filePath, List<Person> list_param)
        {
            IFormatter formatter = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            formatter.Serialize(ms, list_param);  
            byte[] buffer = ms.ToArray();
            ms.Close();
            ms.Dispose();

            FileStream s1 = File.Open(filePath, FileMode.Append);
            GZipStream gzipStream = new GZipStream(s1, CompressionMode.Compress, true); 
            gzipStream.Write(buffer, 0, buffer.Length);
            gzipStream.Close();
        
            s1.Close(); 
            s1.Dispose();
        }

   
        /// 反序列化压缩的DataSet    
        /// </summary>    
        /// <param name="_filePath"></param>    
        /// <returns></returns>    
        static List<Person> DataSetDeserializeDecompress(string _filePath)
        {
            List<Person> listAll = new List<Person>();
            FileStream fs = File.OpenRead(_filePath);
            fs.Position = 0;   
           
            GZipStream gzipStream = new GZipStream(fs, CompressionMode.Decompress,true);//创建解

压对象    
            byte[] buffer = new byte[4096];
            int offset = 0;   
            MemoryStream ms = new MemoryStream();   
           [color=#FF0000] --只能读取出前10个对象的数据流,后10个对象的数据流丢失,但是fs是文件的

全部数据流--[/color]
            while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                ms.Write(buffer, 0, offset);//解压后的数据写入内存流    
            }
   
            BinaryFormatter sfFormatter = new BinaryFormatter();
            ms.Position = 0;    
            List<Person> ed_list;
            try
            {
                while (ms.Position != ms.Length)
                {
                    ed_list = (List<Person>)sfFormatter.Deserialize(ms);
                    listAll.AddRange(ed_list);
                }   
            }
            catch
            {
                throw;
            }
            finally
            {
                ms.Close();  
                ms.Dispose(); 
            }
            fs.Close();
            fs.Dispose();
            gzipStream.Close();   
            gzipStream.Dispose();
            return listAll;
        }

xiaosun的主页 xiaosun | 初学一级 | 园豆:150
提问于:2011-09-23 09:26
< >
分享
所有回答(2)
0

DataSerializerAppend写成这样:

        ///<summary>
/// 序列化对象的追加
///</summary>
///<param name="filePath"></param>
///<param name="ds"></param>
public static void DataSerializerAppend(string filePath, List<Person> list_param)
{
List<Person> temp = DataSetDeserializeDecompress(filePath);
temp.AddRange(list_param);
DirectoryInfo dir=new DirectoryInfo(filePath);
DataSetSerializerCompression(temp, dir.Parent.FullName, dir.Name);
}



artwl | 园豆:16736 (专家六级) | 2011-09-23 10:01
0

建议提问时对代码进行着色。

dudu | 园豆:29294 (高人七级) | 2011-09-23 10:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册