adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.24k stars 480 forks source link

IndexOutOfRangeException #741

Open fmscole opened 1 year ago

fmscole commented 1 year ago

version 0.33 code :

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Factories;
using SharpCompress.Readers;

namespace RarMD5Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            string rarFilePath = "E:\\教案资料2库\\新建文件夹\\高中数学特级教师王新敞精品教案大全.rar";
            RarFactory factory = new RarFactory();
            SharpCompress.Readers.ReaderOptions options = new SharpCompress.Readers.ReaderOptions();
            options.ArchiveEncoding.Default = Encoding.GetEncoding("GBK");
            options.Password = "www.fjmath.com";
            using (Stream stream = File.OpenRead(rarFilePath))
            using (IArchive archive = factory.Open(stream, options))
            {
                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                    {
                        using (Stream entryStream = entry.OpenEntryStream())
                        {
                            string md5 = CalculateMD5(entryStream);
                            Console.WriteLine($"{entry.Key}: {md5}");
                        }
                    }
                }
            }
        }

        public static string CalculateMD5(Stream stream)
        {
            using (var md5 = MD5.Create())
            {
                byte[] hash = md5.ComputeHash(stream);
                return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
            }
        }
    }
}

image

image

adamhathcock commented 1 year ago

Either your password is wrong, encoding is wrong or bad RAR

fmscole commented 1 year ago

@adamhathcock When I use WinRAR to extract, there is no problem, so there is no issue with the password or file. The occurrence of this error is accidental, as it happens only halfway through the reading process and the password must be correct.

Erior commented 1 year ago

we might take a look if we have a file to verify the claim