DotNetZip: Add Files to Dynamically Created Archive Directory

隐身守侯 提交于 2019-12-05 12:04:14

On further inspection, since posting a comment a few minutes ago, I suspect that setting FileName is erasing the archive path.

Testing confirms this.

Setting the name to @"NewDirectory\CustomName2.doc" will fix the problem.

You can also use @"\NewDirectory\CustomName2.doc"

Not sure if this exactly suites your needs but thought I would share. It is a method that is part of a helper class that I created to make working with DotNetZip a bit easier for my dev team. The IOHelper class is another simple helper class that you can ignore.

    /// <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();
        }
    }    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!