如何解决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 举报,一经查实,本站将立刻删除。