我有两个函数,保存、读取object对象到磁盘文件。文件平均大小200M。
大神给封装下。保存和读取文件的时候压缩下。。
压缩幅度越大越好,尽量把文件压缩的小点。 public static void SaveObject(object o, string filePath)
{
if (!Directory.Exists(filePath)) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)); }
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, o);
}
} public static object ReadObject(string filePath)
{
object o;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
o = bf.Deserialize(fs) as object;
}
return o;
}
大神给封装下。保存和读取文件的时候压缩下。。
压缩幅度越大越好,尽量把文件压缩的小点。 public static void SaveObject(object o, string filePath)
{
if (!Directory.Exists(filePath)) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)); }
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, o);
}
} public static object ReadObject(string filePath)
{
object o;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
o = bf.Deserialize(fs) as object;
}
return o;
}
解决方案 »
- C#接口中是否有虚函数?
- SQL存储过程就实现删除指定Access数据库的问题!!
- 关于属性,我怎么才能在一个方法运行后得到的值能在另一个方法里使用呢
- 帮个忙,我在程序中写了一条修改表的语句,运行之后,总是提示@ColumnName附近有错误.
- 请问C#里面有"变体"这种类型吗?
- Excel能导入到datagrid中,但不知道怎么保存到Access数据库。
- 用c#实现面向对象多态的问题,谢谢了!(在线等待!!)
- PictureBox重绘为何要使用System.Windows.Forms.PaintEventArgs e而不能直接引用名称?
- 已装VS2002后安装VS2003时的错误,UP有分!!!
- 100分,请推荐初学C#应该看的书?
- 请教:用空格或Tab分割字符串,但忽略双引号内的内容
- C# Datetime格式的数据,怎么去掉时分秒,只要年月日。
public class ZipHelper
{
public static void Zip(string zipFilePath, string passWord = null, params string[] files)
{
using (var fStream = File.Create(zipFilePath))
{
using (var zoStream = new ZipOutputStream(fStream))
{
if (!string.IsNullOrWhiteSpace(passWord))
{
zoStream.Password = passWord;
}
var buffer = new byte[4096];
foreach (var file in files)
{
var entry = new ZipEntry(Path.GetFileName(file));
zoStream.PutNextEntry(entry);
using (var fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
zoStream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
zoStream.Finish();
}
}
} public static IList<string> UnZip(string zipFilePath, string unZipDirectory, string passWord = null)
{
IList<string> list = new List<string>();
using (var ziStream = new ZipInputStream(File.OpenRead(zipFilePath)))
{
if (!string.IsNullOrWhiteSpace(passWord))
{
ziStream.Password = passWord;
}
ZipEntry theEntry;
while ((theEntry = ziStream.GetNextEntry()) != null)
{
if (!Directory.Exists(unZipDirectory))
{
Directory.CreateDirectory(unZipDirectory);
}
string path = Path.Combine(unZipDirectory, theEntry.Name);
using (var streamWriter = File.Create(path))
{
list.Add(path);
var data = new byte[2048];
while (true)
{
var size = ziStream.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
return list;
}
}
这个必须是成员都必须是简单数据类型,不然还包括一些引用,那些引用对象可能早不存在了,可能就完全错误了 public static byte[] Object2Bytes(object obj)
{
byte[] buff = new byte[Marshal.SizeOf(obj)];
IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
Marshal.StructureToPtr(obj, ptr, true);
return buff;
} public static object Bytes2Object(byte[] buff, Type typ)
{
IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
return Marshal.PtrToStructure(ptr, typ);
}
压缩本质是对byte[]进行处理
所以最高效的方法是自行对object的特性进行分析
根据数据特征进行压缩。
比如,存入的是一组相邻的时间(Datetime)
要压缩效果好,
就是存一个开始时间,后续的数据存与起始时间的差值