看起来像一个文档错误。我为此打开了两个 GitHub 问题,one for the source code comments 和 one 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);
}
}
看起来docs.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);
...
强调:
// 请注意,即使destinationDirectoryName 存在,这也会为我们提供良好的DirectoryInfo