Why I Received Data Error Exception on 7-zip SDK? winrar 5 decompressing

503 views Asked by At

I attached special winrar archive which sdk give me error. Please, can you help me?

archive for simulate bug and test

call example:

 MemoryStream inputMemoryStream = new MemoryStream();
 string archiveFilePath = @"c:\winrar.rar";
 File.OpenRead(archiveFilePath).CopyTo(inputMemoryStream);
 var bb = ExtractBytes(inputMemoryStream.ToArray());



private byte[] ExtractBytes(byte[] data)
{
        using (var inStream = new MemoryStream(data))
        {
            var decoder = new SevenZip.Compression.LZMA.Decoder();
            inStream.Seek(0, 0);
            using (var outStream = new MemoryStream())
            {
                long outSize;
                decoder.SetDecoderProperties(GetLzmaProperties(inStream, out outSize));
                decoder.Code(inStream, outStream, inStream.Length - inStream.Position, outSize, null);
                return outStream.ToArray();
            }
        }
 }
    
     private byte[] GetLzmaProperties(Stream inStream, out long outSize)
    {
        var lzmAproperties = new byte[5];
        if (inStream.Read(lzmAproperties, 0, 5) != 5)
        {
            throw new Exception("LzmaException");
        }
        outSize = 0;
        for (int i = 0; i < 8; i++)
        {
            int b = inStream.ReadByte();
            if (b < 0)
            {
                throw new Exception("LzmaException");
            }
            outSize |= ((long)(byte)b) << (i << 3);
        }
        return lzmAproperties;
    }

Exception is happing when call code function:

decoder.Code(inStream, outStream, inStream.Length - inStream.Position, outSize, null);
            

https://www.7-zip.org/sdk.html ver.19.00

exception debugging details enter image description here

desired behavior: 7zip support winrar, then what I am going wrong in my code?

archive format info: https://superuser.com/questions/770370/what-is-the-difference-between-rar-and-rar5-compression

1

There are 1 answers

0
user31381 On

LZMA SDK cannot be used to extract RAR archives:

  • RAR is not using LZMA as the compression algorithm, and RAR is not supported by LZMA SDK
  • 7-Zip and LZMA SDK are not the same thing
  • 7-Zip uses the unrar code from RARLAB, which is proprietary