popcorn
popcorn

Reputation: 408

LZMA Compression options in C#

I need to get these options for LZMA in Python:

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,
            }
        ],
    )

Into C#.

So far I have got this:

        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

However if I compress the same input in both languages I get different output bytes:

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)

I am using the 7Zip LZMA SDK NuGet package for C#. I think it is because there are some properties that are set differently. What properties for the LZMA Compressor should I change in C# to get the same output as in Python?

Upvotes: 0

Views: 1680

Answers (0)

Related Questions