我有两个函数,保存、读取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;
        }

解决方案 »

  1.   

    zip压缩啊…… SharpZipLib
        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;
            }
        }
      

  2.   

    object要能保存为文件,还要能读回来还原,有一个前提是
    这个必须是成员都必须是简单数据类型,不然还包括一些引用,那些引用对象可能早不存在了,可能就完全错误了    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);
        }
      

  3.   

    保存的是list<T>这样的数据。应该可以压缩保存的吧
      

  4.   

    通用压缩算法调用简单,但是压缩效率可能达不到预期。
    压缩本质是对byte[]进行处理
    所以最高效的方法是自行对object的特性进行分析
    根据数据特征进行压缩。
    比如,存入的是一组相邻的时间(Datetime)
    要压缩效果好,
    就是存一个开始时间,后续的数据存与起始时间的差值