ZipFile.OpenRead(String) Méthode

Définition

Ouvre une archive zip pour la lecture sur le chemin spécifié.

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

Paramètres

archiveFileName
String

Chemin d’accès à l’archive à ouvrir, spécifié en tant que chemin relatif ou absolu. Un chemin relatif est interprété comme relatif au répertoire de travail actuel.

Retours

Archive zip ouverte.

Exceptions

archiveFileName est Empty, contient uniquement un espace blanc ou contient au moins un caractère non valide.

archiveFileName a la valeur null.

Dans archiveFileName, le chemin d’accès spécifié, le nom de fichier ou les deux dépassent la longueur maximale définie par le système.

archiveFileName n’est pas valide ou n’existe pas (par exemple, il se trouve sur un lecteur non mappé).

archiveFileName impossible d’ouvrir.

-ou-

Une erreur d’E/S non spécifiée s’est produite lors de l’ouverture du fichier.

archiveFileName spécifie un répertoire.

-ou-

L’appelant n’a pas l’autorisation requise pour accéder au fichier spécifié dans archiveFileName.

Le fichier spécifié dans archiveFileName est introuvable.

archiveFileName contient un format non valide.

archiveFileName n’a pas pu être interprété comme une archive zip.

Exemples

L’exemple suivant montre comment ouvrir une archive zip pour la lecture.

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

Remarques

Cette méthode équivaut à appeler la Open méthode et à définir le mode paramètre sur Read. L’archive est ouverte avec FileMode.Open la valeur du mode fichier. Si l’archive n’existe pas, une FileNotFoundException exception est levée.

S’applique à