سؤال

I have used NUnrar to extract my files :

NUnrar.Archive.RarArchive archive = NUnrar.Archive.RarArchive.Open(location + "1.rar");

foreach (RarArchiveEntry item in archive.Entries)
{
    string path = Path.Combine(location, Path.GetFileName(item.FilePath));
    item.WriteToFile(path);
}

If my file has no any sub directory all things works but if rar file has sub directory all of them extracted to same folder how i can keep model of sub directory and file places

هل كانت مفيدة؟

المحلول

I've had to do some experimenting to get NUnrar to work properly as well. Perhaps the little success I had can help you.

RarArchive archive = RarArchive.Open(@"D:\Archives\Test.rar");
foreach (RarArchiveEntry entry in archive.Entries)
{
    try
    {
        string fileName = Path.GetFileName(entry.FilePath);
        string rootToFile = Path.GetFullPath(entry.FilePath).Replace(fileName, "");

        if (!Directory.Exists(rootToFile))
        {
            Directory.CreateDirectory(rootToFile);
        }

        entry.WriteToFile(rootToFile + fileName, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
    }
    catch (Exception ex)
    {
        //handle your exception here..
    }
}

The code I have already uses (Exception e) further up, so I had to use (Exception ex) instead. It's probably sloppy code, and could do with a tidy up - but being as late as it is I'm inclined to leave it be as it 'works'..

نصائح أخرى

NUnrar.Archive.RarArchive.WriteToDirectory("update.rar", Application.StartupPath,NUnrar.Common.ExtractOptions.ExtractFullPath | NUnrar.Common.ExtractOptions.Overwrite);

if "update.rar" is inside the same directory as the executable.

I'd think RarArchive.ExtractToDirectory(source, destination); should work.

Or using your loop, change it to string path = Path.Combine(location, item.FilePath);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top