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 か、空白のみが含まれているか、無効な文字が少なくとも 1 つ含まれています。

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);
                }
            }
        }
    }
}
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 に設定することとmodeRead同じです。 アーカイブは、ファイル モードの値として で FileMode.Open 開かれます。 アーカイブが存在しない場合は、 FileNotFoundException 例外がスローされます。

適用対象