【问题标题】:DotNetZip: Add Files to Dynamically Created Archive DirectoryDotNetZip:将文件添加到动态创建的存档目录
【发布时间】:2012-02-04 14:22:37
【问题描述】:

我无法想象这很难做到,但我无法让它发挥作用。我有一个文件类,它只存储我要压缩的文件的位置、目录和名称。我正在压缩的文件存在于磁盘上,因此 FileLocation 是完整路径。磁盘上不存在 ZipFileDirectory。如果我的文件列表中有两个项目,

{ FileLocation = "path/file1.doc", ZipFileDirectory = @"\", FileName = "CustomName1.doc" },

{ FileLocation = "path/file2.doc", ZipFileDirectory = @"\NewDirectory", FileName = "CustomName2.doc" }

我希望在根目录中看到 MyCustomName1.doc,以及一个名为 NewDirectory 的文件夹,其中包含 MyCustomName2.doc,但结果是它们都使用以下代码在根目录中结束:

using (var zip = new Ionic.Zip.ZipFile())
{
    foreach (var file in files)
    {
        zip.AddFile(file.FileLocation, file.ZipFileDirectory).FileName = file.FileName;
    }

    zip.Save(HttpContext.Current.Response.OutputStream);
}

如果我使用这个:

zip.AddFiles(files.Select(o => o.FileLocation), false, "NewDirectory");

然后它创建新目录并将所有文件放入其中,正如预期的那样,但随后我失去了使用此方法使用自定义命名的能力,并且它还引入了第一种方法可以完美处理的更多复杂性。

有没有办法让第一个方法(AddFile())按我的预期工作?

【问题讨论】:

  • 我正在查看 DotNetZip 代码,看起来 AddFile() 实际上应该按您的预期工作。我正在考虑您应该将FileName 设置为“NewDirectory\CustomName2.doc”的假设,但这不受代码支持。但是,这可能取决于版本(可能是错误)。你用的是什么版本?

标签: c# dotnetzip


【解决方案1】:

进一步检查,自从几分钟前发表评论以来,我怀疑设置 FileName 正在擦除存档路径。

测试证实了这一点。

将名称设置为@"NewDirectory\CustomName2.doc" 将解决问题。

你也可以使用@"\NewDirectory\CustomName2.doc"

【讨论】:

    【解决方案2】:

    不确定这是否完全符合您的需求,但我想我会分享。它是我创建的帮助程序类的一部分,旨在让我的开发团队更轻松地使用 DotNetZip。 IOHelper 类是另一个您可以忽略的简单助手类。

        /// <summary>
        /// Create a zip file adding all of the specified files.
        /// The files are added at the specified directory path in the zip file.
        /// </summary>
        /// <remarks>
        /// If the zip file exists then the file will be added to it.
        /// If the file already exists in the zip file an exception will be thrown.
        /// </remarks>
        /// <param name="filePaths">A collection of paths to files to be added to the zip.</param>
        /// <param name="zipFilePath">The fully-qualified path of the zip file to be created.</param>
        /// <param name="directoryPathInZip">The directory within the zip file where the file will be placed.
        /// Ex. specifying "files\\docs" will add the file(s) to the files\docs directory in the zip file.</param>
        /// <param name="deleteExisting">Delete the zip file if it already exists.</param>
        public void CreateZipFile(ICollection<FileInfo> filePaths, string zipFilePath, string directoryPathInZip, bool deleteExisting)
        {
            if (deleteExisting)
            {
                IOHelper ioHelper = new IOHelper();
                ioHelper.DeleteFile(zipFilePath);
            }
    
            using (ZipFile zip = new ZipFile(zipFilePath))
            {
                foreach (FileInfo filePath in filePaths)
                {
                    zip.AddFile(filePath.FullName, directoryPathInZip);
                }
                zip.Save();
            }
        }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多