【问题标题】:Ionic.Zip ArgumentException (An item with the same key has already been added)Ionic.Zip ArgumentException(已添加具有相同密钥的项目)
【发布时间】:2017-05-14 07:12:42
【问题描述】:

我正在使用 Ionic Zip 压缩特定文件夹中的所有文件夹,不包括备份文件夹(创建备份的文件夹)。

这是我的代码:

ZipFile zip = new ZipFile();
string mainpath = HttpContext.Current.Server.MapPath("~/");
Directory.GetDirectories(mainpath).Where(d=> !d.ToLower().EndsWith("backup")).ToList()
.ForEach(d=> zip.AddDirectory(d));

但添加一些目录后,我收到以下错误:

已添加具有相同密钥的项目。

这怎么可能?同一个父文件夹中的文件夹列表怎么可能有重名?

【问题讨论】:

    标签: c# io ionic-zip


    【解决方案1】:

    当我检查异常时,它似乎添加了要在字典中存档的所有文件的列表。

    我不知道它使用什么作为可能导致此错误的键(可能使用文件名作为键并且在两个不同的文件夹中具有相同的名称可能会导致此错误)。

    解决方案: 但是我发现AddDirectoryAddFiles 有另一个重载,它接受存档中的目录路径。给每个目录一个唯一的存档路径解决了这个问题。在我的情况下,我使用:

        string mainpath = HttpContext.Current.Server.MapPath("~/");
        Directory.GetDirectories(mainpath).Where(d=> !d.ToLower().EndsWith("backup")).ToList()
    .ForEach(d=> zip.AddDirectory(d, d.Substring(mainpath.Length)));
    

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 2011-01-27
      • 2021-08-02
      • 2011-03-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多