Unrar文件带子目录

时间:2012-10-26 13:19:22

标签: c# rar

我使用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文件有子目录,所有这些都被提取到同一个文件夹,我怎么能保持子目录和文件位置的模型

3 个答案:

答案 0 :(得分: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..
    }
}

我已经使用的代码(例外e),所以我不得不使用(Exception ex)代替。 它可能是邋code的代码,并且可以用一个整洁的方式 - 但是尽管它很晚,我倾向于留下它,因为它“工作”..

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

I'd think RarArchive.ExtractToDirectory(source, destination);应该有用。

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