BinaryFormatter 是否应用任何压缩?

发布于 2024-08-03 07:24:25 字数 329 浏览 11 评论 0原文

当 .NET 的 BinaryFormatter用于序列化对象图,是否应用了任何类型的压缩?

我问是否应该担心对象图有许多重复的字符串和整数。

编辑 - 等等,如果字符串驻留在.NET中,就不需要担心重复的字符串,对吧?

When .NET's BinaryFormatter is used to serialize an object graph, is any type of compression applied?

I ask in the context of whether I should worry about the object graph having many repeated strings and integers.

Edit - Hold on, if strings are interned in .NET, there's no need to worry about repeated strings, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年不长 2024-08-10 07:24:25

不,它不提供任何压缩,但您可以使用 GZipStream 类型。

编辑: Mehrdad 在他对 如何使用 gzip 压缩 .net 对象实例

编辑 2: 字符串可以被保留,但这并不意味着每个字符串被保留。我不会对 CLR 如何或为何决定实习字符串做出任何假设,因为这可能会随着版本的不同而改变(并且已经改变)。

No, it doesn't provide any compression but you can compress the output yourself using the GZipStream type.

Edit: Mehrdad has a wonderful example of this technique in his answer to How to compress a .net object instance using gzip.

Edit 2: Strings can be interned but that doesn't mean that every string is interned. I wouldn't make any assumptions on how or why the CLR decides to intern strings as this can change (and has changed) from version to version.

初懵 2024-08-10 07:24:25

不,它没有,但是......

我今天刚刚为我的应用程序添加了 GZipStream 支持,所以我可以在这里分享一些代码;

序列化:

using (Stream s = File.Create(PathName))
{
    RijndaelManaged rm = new RijndaelManaged();
    rm.Key = CryptoKey;
    rm.IV = CryptoIV;
    using (CryptoStream cs = new CryptoStream(s, rm.CreateEncryptor(), CryptoStreamMode.Write))
    {
        using (GZipStream gs = new GZipStream(cs, CompressionMode.Compress))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(gs, _instance);
        }
    }
}

反序列化:

using (Stream s = File.OpenRead(PathName))
{
    RijndaelManaged rm = new RijndaelManaged();
    rm.Key = CryptoKey;
    rm.IV = CryptoIV;
    using (CryptoStream cs = new CryptoStream(s, rm.CreateDecryptor(), CryptoStreamMode.Read))
    {
        using (GZipStream gs = new GZipStream(cs, CompressionMode.Decompress))
        {
            BinaryFormatter bf = new BinaryFormatter();
            _instance = (Storage)bf.Deserialize(gs);
        }
    }
}

注意:如果您使用 CryptoStream,以这种方式链接(解开)压缩和(解密)加密非常重要,因为您需要在加密从数据中产生噪音之前丢失熵。

No, it does not, but...

I just added GZipStream support for my app today, so I can share some code here;

Serialization:

using (Stream s = File.Create(PathName))
{
    RijndaelManaged rm = new RijndaelManaged();
    rm.Key = CryptoKey;
    rm.IV = CryptoIV;
    using (CryptoStream cs = new CryptoStream(s, rm.CreateEncryptor(), CryptoStreamMode.Write))
    {
        using (GZipStream gs = new GZipStream(cs, CompressionMode.Compress))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(gs, _instance);
        }
    }
}

Deserialization:

using (Stream s = File.OpenRead(PathName))
{
    RijndaelManaged rm = new RijndaelManaged();
    rm.Key = CryptoKey;
    rm.IV = CryptoIV;
    using (CryptoStream cs = new CryptoStream(s, rm.CreateDecryptor(), CryptoStreamMode.Read))
    {
        using (GZipStream gs = new GZipStream(cs, CompressionMode.Decompress))
        {
            BinaryFormatter bf = new BinaryFormatter();
            _instance = (Storage)bf.Deserialize(gs);
        }
    }
}

NOTE: if you use CryptoStream, it is kinda important that you chain (un)zipping and (de)crypting right this way, because you'll want to lose your entropy BEFORE encryption creates noise from your data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文