ZipFile.OpenRead(String) 方法

定义

打开在指定路径用于读取的 zip 存档。

public:
 static System::IO::Compression::ZipArchive ^ OpenRead(System::String ^ archiveFileName);
public static System.IO.Compression.ZipArchive OpenRead (string archiveFileName);
static member OpenRead : string -> System.IO.Compression.ZipArchive
Public Shared Function OpenRead (archiveFileName As String) As ZipArchive

参数

archiveFileName
String

要打开的存档的路径,指定为相对路径或绝对路径。 相对路径被解释为相对于当前工作目录。

返回

ZipArchive

打开的 zip 存档。

例外

archiveFileNameEmpty,仅包含空格,或者包含至少一个无效字符。

archiveFileNamenull

archiveFileName 内,指定的路径、文件名或者两者都超出了系统定义的最大长度。

archiveFileName 无效或不存在(例如,在未映射的驱动器上)。

未能打开 archiveFileName

  • 或 -

打开文件时发生未指定的 I/O 错误。

archiveFileName 指定目录。

  • 或 - 调用方没有访问在 archiveFileName 中指定的文件的所需权限。

未找到 archiveFileName 中指定的文件。

archiveFileName 包含无效格式。

archiveFileName 无法解释为 zip 存档文件。

示例

以下示例演示如何打开 zip 存档进行读取。

using System;
using System.IO;
using System.IO.Compression;

class Program
{
    static void Main(string[] args)
    {
        string zipPath = @".\result.zip";

        Console.WriteLine("Provide path where to extract the zip file:");
        string extractPath = Console.ReadLine();

        // Normalizes the path.
        extractPath = Path.GetFullPath(extractPath);

        // Ensures that the last character on the extraction path
        // is the directory separator char.
        // Without this, a malicious zip file could try to traverse outside of the expected
        // extraction path.
        if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
            extractPath += Path.DirectorySeparatorChar;

        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // Gets the full path to ensure that relative segments are removed.
                    string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName));

                    // Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                    // are case-insensitive.
                    if (destinationPath.StartsWith(extractPath, StringComparison.Ordinal))
                        entry.ExtractToFile(destinationPath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = ".\result.zip"

        Console.WriteLine("Provide path where to extract the zip file:")
        Dim extractPath As String = Console.ReadLine()

        ' Normalizes the path.
        extractPath = Path.GetFullPath(extractPath)

        ' Ensures that the last character on the extraction path
        ' is the directory separator char. 
        ' Without this, a malicious zip file could try to traverse outside of the expected
        ' extraction path.
        If Not extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) Then
            extractPath += Path.DirectorySeparatorChar
        End If

        Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
            For Each entry As ZipArchiveEntry In archive.Entries
                If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then

                    ' Gets the full path to ensure that relative segments are removed.
                    Dim destinationPath As String = Path.GetFullPath(Path.Combine(extractPath, entry.FullName))
                    
                    ' Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                    ' are case-insensitive.
                    If destinationPath.StartsWith(extractPath, StringComparison.Ordinal) Then 
                        entry.ExtractToFile(destinationPath)
                    End If

                End If
            Next
        End Using
    End Sub

End Module

注解

此方法等效于调用 Open 该方法并将参数设置为 mode Read. 存档以文件模式值的形式打开 FileMode.Open 。 如果存档不存在, FileNotFoundException 则会引发异常。

适用于