描述是这样:首先我先序列化10个对象,并压缩,存到文件中,然后我又追加了10对象,也序列化,并压缩追加到文件中,然后我反序列化时,先全部读取,然后再解压,然后再反序列化;
出错问题:当数据解压时,只解压了前10个对象的数据流,后面追加的没有了;下面是实现代码:///序列化
static void DataSetSerializerCompression(List<Person> list, string savePath, string saveName)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, list);
//XmlSerializer xs = new XmlSerializer(typeof(List<Person>));
//xs.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>
/// 序列化对象的追加
/// </summary>
/// <param name="filePath"></param>
/// <param name="list_param"></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();
}
/// <summary>
/// 反序列化解压缩
/// </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;
--fs流已经读取出全部的压缩数据---
GZipStream gzipStream = new GZipStream(fs, CompressionMode.Decompress,true);
byte[] buffer = new byte[4096];
int offset = 0;
MemoryStream ms = new MemoryStream();
----下面这句,解压缩的时候只解压了前10个,后面追加的10个丢失了----
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;
}
出错问题:当数据解压时,只解压了前10个对象的数据流,后面追加的没有了;下面是实现代码:///序列化
static void DataSetSerializerCompression(List<Person> list, string savePath, string saveName)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, list);
//XmlSerializer xs = new XmlSerializer(typeof(List<Person>));
//xs.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>
/// 序列化对象的追加
/// </summary>
/// <param name="filePath"></param>
/// <param name="list_param"></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();
}
/// <summary>
/// 反序列化解压缩
/// </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;
--fs流已经读取出全部的压缩数据---
GZipStream gzipStream = new GZipStream(fs, CompressionMode.Decompress,true);
byte[] buffer = new byte[4096];
int offset = 0;
MemoryStream ms = new MemoryStream();
----下面这句,解压缩的时候只解压了前10个,后面追加的10个丢失了----
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;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货