Модуль Python 3.x lzma создает несовместимые данные из других реализаций языка, таких как C#, javascript?
Я использую модуль lzma из python 3.x:
compressed = lzma.compress(data=b'\x97', format=lzma.FORMAT_ALONE, preset=1)
print(":".join(["{0:02X}".format(c) for c in compressed]))
ВЫХОД:
'5D:00:00:10:00:FF:FF:FF:FF:FF:FF:FF:FF:00:4B:C1:FB:FF:FF:FF:E0:00:00:00'
и я использую JavaScript LZMA-JS, это выход:
"5D:00:00:01:00:01:00:00:00:00:00:00:00:00:30:C1:FB:FF:FF:FF:E0:00:00:00"
КОД:
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16).toUpperCase()).slice(-2);
}).join(':')
}
toHexString(LZMA.compress("a", 1))
и я использую 7zip LZMA-SDK, он выводит:
5D:00:00:01:00:01:00:00:00:00:00:00:00:00:30:C1:FB:FF:FF:FF:E0:00:00:00
КОД:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(BitConverter.ToString(CompressLzma("a")).Replace('-', ':'));
}
public static byte[] CompressLzma(string inputstring)
{
if (!string.IsNullOrEmpty(inputstring))
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(inputstring ?? ""));
var outputStream = new MemoryStream();
Compress(stream, outputStream);
byte[] bytes = outputStream.ToArray();
return bytes;
}
return null;
}
public static void PrepareEncoder(out CoderPropID[] propIDs, out object[] properties)
{
bool eos = true;
Int32 dictionary = 1 << 16;
Int32 posStateBits = 2;
Int32 litContextBits = 3; // for normal files
// UInt32 litContextBits = 0; // for 32-bit data
Int32 litPosBits = 0;
// UInt32 litPosBits = 2; // for 32-bit data
Int32 algorithm = 2;
Int32 numFastBytes = 64;
string mf = "bt4";
propIDs = new CoderPropID[]
{
CoderPropID.DictionarySize,
CoderPropID.PosStateBits,
CoderPropID.LitContextBits,
CoderPropID.LitPosBits,
CoderPropID.Algorithm,
CoderPropID.NumFastBytes,
CoderPropID.MatchFinder,
CoderPropID.EndMarker
};
properties = new object[]
{
dictionary,
posStateBits,
litContextBits,
litPosBits,
algorithm,
numFastBytes,
mf,
eos
};
}
public static void Compress(MemoryStream inStream, MemoryStream outStream)
{
CoderPropID[] propIDs;
object[] properties;
PrepareEncoder(out propIDs, out properties);
SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
encoder.SetCoderProperties(propIDs, properties);
encoder.WriteCoderProperties(outStream);
Int64 fileSize = inStream.Length;
for (int i = 0; i < 8; i++)
{
outStream.WriteByte((Byte)(fileSize >> (8 * i)));
}
encoder.Code(inStream, outStream, -1, -1, null);
}
}
JS SDK и.NET SDK выдают одинаковые сжатые данные, но Python SDK выдает разные данные, которые не могут быть распакованы другим языком SDK.
соответствующие ссылки: