ZipFile.OpenRead(String) ZipFile.OpenRead(String) ZipFile.OpenRead(String) ZipFile.OpenRead(String) Method

Definice

Otevře archiv zip pro čtení v zadané cestě.Opens a zip archive for reading at the specified path.

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

Parametry

archiveFileName
String String String String

Cesta k archivu, která se má otevřít, zadaná jako relativní nebo absolutní cesta.The path to the archive to open, specified as a relative or absolute path. Relativní cesta je interpretována jako relativní k aktuálnímu pracovnímu adresáři.A relative path is interpreted as relative to the current working directory.

Návraty

Otevřený archiv zip.The opened zip archive.

Výjimky

archiveFileNameje Empty, obsahuje pouze prázdné znaky nebo obsahuje nejméně jeden neplatný znak.archiveFileName is Empty, contains only white space, or contains at least one invalid character.

V archiveFileNamesystému je zadaná cesta, název souboru nebo obojí větší než maximální délka definovaná systémem.In archiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

archiveFileNameje neplatný nebo neexistuje (například se nachází na nemapované jednotce).archiveFileName is invalid or does not exist (for example, it is on an unmapped drive).

archiveFileNameNelze otevřít.archiveFileName could not be opened.

archiveFileNameUrčuje adresář.archiveFileName specifies a directory.

-nebo--or- Volající nemá požadovaná oprávnění pro přístup k souboru zadanému v archiveFileName.The caller does not have the required permission to access the file specified in archiveFileName.

Soubor zadaný v archiveFileName souboru nebyl nalezen.The file specified in archiveFileName is not found.

archiveFileNameobsahuje neplatný formát.archiveFileName contains an invalid format.

archiveFileNamenelze interpretovat jako archiv zip.archiveFileName could not be interpreted as a zip archive.

Příklady

Následující příklad ukazuje, jak otevřít archiv zip pro čtení.The following example shows how to open a zip archive for reading.

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

Poznámky

Tato metoda je ekvivalentní volání Open metody a mode nastavení parametru na Read.This method is equivalent to calling the Open method and setting the mode parameter to Read. Archiv je otevřen s FileMode.Open jako hodnota režimu souboru.The archive is opened with FileMode.Open as the file mode value. Pokud archiv neexistuje, FileNotFoundException je vyvolána výjimka.If the archive does not exist, a FileNotFoundException exception is thrown.

Platí pro