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

열 보관 위치의 경로(상대 또는 절대 경로로 지정)입니다. 상대 경로는 현재 작업 디렉터리에 상대적으로 해석됩니다.

반환

열린 zip 보관 위치입니다.

예외

archiveFileNameEmpty이거나, 공백만 포함하거나 또는 잘못된 문자를 하나 이상 포함하는 경우

archiveFileName이(가) null인 경우

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);
                }
            }
        }
    }
}
open System
open System.IO;
open System.IO.Compression;

[<EntryPoint>]
let main _ =
    let zipPath = @".\result.zip"

    printfn "Provide path where to extract the zip file:"
    let extractPath = stdin.ReadLine();

    // Normalizes the path.
    let mutable 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) |> not then
        extractPath <- extractPath + string Path.DirectorySeparatorChar

    use archive = ZipFile.OpenRead zipPath

    for entry in archive.Entries do
        if entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) then
            // Gets the full path to ensure that relative segments are removed.
            let 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) then
                entry.ExtractToFile destinationPath
    0
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 throw됩니다.

적용 대상