我需要在 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)(2),
(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, -1, null);
retVal = strmOutStream.ToArray();
} // End Using outStream
} // End Using inStream
return retVal;
} // End Function Compress
但是,如果我用两种语言压缩相同的输入,我会得到不同的输出字节:
Python:
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 中相同的输出?