为什么我在 7-zip SDK 上收到数据错误异常? winrar 5 解压

问题描述

我附上了特殊的 winrar 存档,sdk 给了我错误。拜托,你能帮我吗?

archive for simulate bug and test

调用示例:

 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,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;
    }

调用代码函数时发生异常:

decoder.Code(inStream,null);
            

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

异常调试详情

enter image description here

期望的行为:7zip 支持 winrar,那么我的代码出了什么问题?

存档格式信息:https://superuser.com/questions/770370/what-is-the-difference-between-rar-and-rar5-compression

解决方法

LZMA SDK 不能用于提取 RAR 压缩文件:

  • RAR 没有使用 LZMA 作为压缩算法,并且 LZMA SDK 不支持 RAR
  • 7-Zip 和 LZMA SDK 不是一回事
  • 7-Zip 使用来自 RARLAB 的 unrar 代码,这是专有的