2

我使用NUnrar来提取我的文件:

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

如果我的文件没有任何子目录,一切正常,但如果 rar 文件有子目录,所有这些子目录都提取到同一个文件夹,我如何保留子目录和文件位置的模型

4

3 回答 3

4

我必须做一些实验才能让 NUnrar 也能正常工作。也许我所取得的小小的成功可以帮助到你。

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..
    }
}

我已经使用了 (Exception e) 的代码,所以我不得不使用 (Exception ex) 代替。这可能是草率的代码,并且可以进行整理-但是到现在为止,我倾向于将其保留为“有效”的状态..

于 2012-10-26T23:19:31.717 回答
1
NUnrar.Archive.RarArchive.WriteToDirectory("update.rar", Application.StartupPath,NUnrar.Common.ExtractOptions.ExtractFullPath | NUnrar.Common.ExtractOptions.Overwrite);

如果“update.rar”与可执行文件位于同一目录中。

于 2014-10-03T07:53:40.557 回答
0

我认为 RarArchive.ExtractToDirectory(source, destination);应该工作。

或使用您的循环,将其更改为string path = Path.Combine(location, item.FilePath);

于 2012-10-26T19:30:48.563 回答