【问题标题】:Unrar file with subdirectory带有子目录的解压缩文件
【发布时间】:2012-10-16 18:25:38
【问题描述】:

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

【问题讨论】:

    标签: c# rar


    【解决方案1】:

    I'd thinkRarArchive.ExtractToDirectory(source, destination); 应该可以工作。

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

    【讨论】:

      【解决方案2】:

      我必须做一些试验才能让 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) 代替。 这可能是草率的代码,并且可以进行整理 - 但迟到了,我倾向于让它保持“工作”状态..

      【讨论】:

      • 最新版本中没有entry.FilePath这个字段,改用entry.Key。
      【解决方案3】:
      NUnrar.Archive.RarArchive.WriteToDirectory("update.rar", Application.StartupPath,NUnrar.Common.ExtractOptions.ExtractFullPath | NUnrar.Common.ExtractOptions.Overwrite);
      

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

      【讨论】:

      • 这是最简单的方法!不需要像 Frankish 那样复杂。
      猜你喜欢
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 2015-12-26
      • 2014-10-23
      相关资源
      最近更新 更多