c# - 当目录存在时 ZipFile.ExtractToDirectory 是否抛出异常?

标签 c# .net .net-core .net-5

根据Microsoft Docs ,如果要提取 zip 文件的目标目录已存在,程序应抛出 IOException。此外,“备注”部分指出“目标目录不能已存在”。然而,

ZipFile.ExtractToDirectory("archive.zip", ".\\output");
即使 output 作为目录存在,

也能正常工作(没有异常(exception))。类似的东西

ZipFile.ExtractToDirectory("archive.zip", "\\");

也无异常(exception)地工作。因此,我试图理解为什么当 Microsoft 文档明确建议应该有异常(exception)时却没有异常(exception)。

注意:我正在运行 .NET 5。

最佳答案

看起来像是文档错误。我为此打开了两个 GitHub 问题,one for the source code commentsone for the docs page由它们创建的

.NET Core 是开源的,这意味着我们可以检查实际的源代码以了解发生了什么。

ZipFile.ExtractToDirectory实际上调用 ZipFileExtensions.ExtractToDirectory(ZipArchive, String)方法:

public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName, Encoding? entryNameEncoding, bool overwriteFiles)
{
    if (sourceArchiveFileName == null)
        throw new ArgumentNullException(nameof(sourceArchiveFileName));

    using (ZipArchive archive = Open(sourceArchiveFileName, ZipArchiveMode.Read, entryNameEncoding))
    {
        archive.ExtractToDirectory(destinationDirectoryName, overwriteFiles);
    }
}

the actual code如果目标目录存在,则不会抛出 IOException,即使文档网站是这样说的:

/// <exception cref="IOException">An archive entry?s name is zero-length, contains only whitespace, or contains one or more invalid
/// characters as defined by InvalidPathChars. -or- Extracting an archive entry would have resulted in a destination
/// file that is outside destinationDirectoryName (for example, if the entry name contains parent directory accessors).
/// -or- An archive entry has the same name as an already extracted entry from the same archive.</exception>
public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, bool overwriteFiles)
{
    if (source == null)
        throw new ArgumentNullException(nameof(source));

    if (destinationDirectoryName == null)
        throw new ArgumentNullException(nameof(destinationDirectoryName));

    foreach (ZipArchiveEntry entry in source.Entries)
    {
        entry.ExtractRelativeToDirectory(destinationDirectoryName, overwriteFiles);
    }
}

看起来 learn.microsoft.com 位于实际源文档的后面。

为了完整起见,internal ExtractRelativeToDirectory method明确认为现有目标文件夹是有效的情况:

internal static void ExtractRelativeToDirectory(this ZipArchiveEntry source, string destinationDirectoryName, bool overwrite)
{
    if (source == null)
        throw new ArgumentNullException(nameof(source));

    if (destinationDirectoryName == null)
        throw new ArgumentNullException(nameof(destinationDirectoryName));

    // Note that this will give us a good DirectoryInfo even if destinationDirectoryName exists:
    DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
      ...

强调:

// Note that this will give us a good DirectoryInfo even if destinationDirectoryName exists

关于c# - 当目录存在时 ZipFile.ExtractToDirectory 是否抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67806595/

相关文章:

c# - 路径字符串中的子字符串

c# - .NET Point.IsEmpty 与 IsDefined

c# - 循环遍历 Silverlight DataGrid 中的行

.net - Silverlight 2.0已准备好用于商务应用程序

c# - C# 删除数组中的相同元素

c# - Aes 加密...缺少一个重要部分

c# - 如何在 Windows 上为 .NET 安装 Python

c# - UseSpaPrerendering 从 .net core 3.0 开始被弃用,替代方案是什么?

c# - 通过 PushSharp.Core 发送 Apple 推送通知停止工作

c# - 如何防止验证器在 HttpGet 上运行?