压缩解压object对象

shsyzl007 2019-04-02 10:48:38
我有两个函数,保存、读取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;
}
...全文
202 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
例如我们持久化许多数据对象,经常需要变动内容,动不动就把“整体”给序列化、反序列化这本身就在思路上决定了性能很低。一个 200M 的数据文件中应该管理一大堆分别之后几十 k 字节的小数据,这才实用!而不是什么整体的“压缩”。
  • 打赏
  • 举报
回复
如果频繁需要序列化、反序列化,你的程序花在这上面的额外的反复处理时间还不如用空间来换算。
xian_wwq 2019-04-03
  • 打赏
  • 举报
回复
通用压缩算法调用简单,但是压缩效率可能达不到预期。 压缩本质是对byte[]进行处理 所以最高效的方法是自行对object的特性进行分析 根据数据特征进行压缩。 比如,存入的是一组相邻的时间(Datetime) 要压缩效果好, 就是存一个开始时间,后续的数据存与起始时间的差值
shsyzl007 2019-04-02
  • 打赏
  • 举报
回复
保存的是list<T>这样的数据。应该可以压缩保存的吧
stherix 2019-04-02
  • 打赏
  • 举报
回复
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); }
shsyzl007 2019-04-02
  • 打赏
  • 举报
回复
能把我的两个函数直接封装下么 我弄了半天调试不通过,
  • 打赏
  • 举报
回复
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;
        }
    }

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧