Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# BinaryFormatter是否应用任何压缩?_C#_.net_Serialization_Compression_Binaryformatter - Fatal编程技术网

C# BinaryFormatter是否应用任何压缩?

C# BinaryFormatter是否应用任何压缩?,c#,.net,serialization,compression,binaryformatter,C#,.net,Serialization,Compression,Binaryformatter,当.NET用于序列化对象图时,是否应用了任何类型的压缩 我在上下文中询问是否应该担心对象图有许多重复的字符串和整数 编辑-请稍候,如果字符串在.NET中,则无需担心重复的字符串,对吗?不,它不提供任何压缩,但您可以使用该类型自己压缩输出 编辑:在他的回答中有一个很好的例子说明了这种技巧 编辑2:字符串可以插入,但这并不意味着每个字符串都被插入。我不会对CLR决定如何或为什么插入字符串做任何假设,因为这可能会随着版本的不同而变化(并且已经发生了变化)。不,它不会,但是 我今天刚刚为我的应用程序添加

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

我在上下文中询问是否应该担心对象图有许多重复的字符串和整数


编辑-请稍候,如果字符串在.NET中,则无需担心重复的字符串,对吗?

不,它不提供任何压缩,但您可以使用该类型自己压缩输出

编辑:在他的回答中有一个很好的例子说明了这种技巧


编辑2:字符串可以插入,但这并不意味着每个字符串都被插入。我不会对CLR决定如何或为什么插入字符串做任何假设,因为这可能会随着版本的不同而变化(并且已经发生了变化)。

不,它不会,但是

我今天刚刚为我的应用程序添加了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);
        }
    }
}
反序列化:

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,以这种方式链接(反)压缩和(反)加密是非常重要的,因为您希望在加密产生数据噪音之前丢失熵。

原语愉快地重复,令人厌恶,唯一发生的“压缩”是不间断的空序列(例如在数组中)以字节或整数形式存储,表示序列中的数字。字符串(通常)不重复,类定义不重复,多个对象引用不会导致对象被多次序列化/输出。