C# 中的 LZMA 压缩选项

问题描述

我需要在 Python 中为 LZMA 获取这些选项:

def lzma_compression(input):
    return lzma.compress(
        input,format=lzma.FORMAT_RAW,filters=[
            {
                'id': lzma.FILTER_LZMA1,'lc': 3,'lp': 0,'pb': 2,'dict_size': 128 * 1024,}
        ],)

进入 C#。

到目前为止,我得到了这个:

        static int dictionary = 128 * 1024;
        static bool eos = false;

        static CoderPropID[] propIDs =
                {
                    CoderPropID.DictionarySize,CoderPropID.PosstateBits,CoderPropID.LitContextBits,CoderPropID.LitPosBits,CoderPropID.Algorithm,CoderPropID.NumFastBytes,CoderPropID.MatchFinder,CoderPropID.EndMarker
                };

        // these are the default properties:
        static object[] properties =
                {
                    (system.int32)(dictionary),(system.int32)(2),(system.int32)(3),(system.int32)(1),(system.int32)(128),"bt4",eos
                };

        public static byte[] Compress(byte[] inputBytes)
        {
            byte[] retVal = null;
            SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
            encoder.SetCoderProperties(propIDs,properties);

            using (System.IO.MemoryStream strmInStream = new System.IO.MemoryStream(inputBytes))
            {
                using (System.IO.MemoryStream strmOutStream = new System.IO.MemoryStream())
                {
                    encoder.WriteCoderProperties(strmOutStream);
                    long fileSize = strmInStream.Length;
                    for (int i = 0; i < 8; i++)
                        strmOutStream.WriteByte((byte)(fileSize >> (8 * i)));

                    encoder.Code(strmInStream,strmOutStream,-1,null);
                    retVal = strmOutStream.ToArray();
                } // End Using outStream

            } // End Using inStream 

            return retVal;
        } // End Function Compress

但是,如果我用两种语言压缩相同的输入,我会得到不同的输出字节:

蟒蛇:

output = lzma_compression(b'x83') # b'\x00<\x0e\x02p\x7f\xff\xff\xff\xf8\x00\x00\x00' (13 bytes)

C#

bytes[] input = new bytes[1];
input[0] = 131;
bytes[] output = Compress(input); // output =  \x66x00\x00\x02\x00\x01\x00\x00\x00x00\x00\x00x00\x00\x41\x7f\xfc\x00\x00 (19 bytes)

我使用的是 C# 的 7Zip LZMA SDK NuGet 包。我认为这是因为有些属性设置不同。我应该在 C# 中更改 LZMA 压缩器的哪些属性以获得与 Python 中相同的输出

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)