ZipFileExtensions ZipFileExtensions ZipFileExtensions ZipFileExtensions Class

Définition

Fournit les méthodes d'extension pour les classes ZipArchive et ZipArchiveEntry .Provides extension methods for the ZipArchive and ZipArchiveEntry classes.

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
Héritage
ZipFileExtensionsZipFileExtensionsZipFileExtensionsZipFileExtensions

Exemples

L’exemple suivant montre comment créer une nouvelle entrée dans une archive zip à partir d’un fichier existant et comment extraire le contenu de l’archive dans un répertoire.The following example shows how to create a new entry in a zip archive from an existing file, and extract the contents of the archive to a directory.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\users\exampleuser\start.zip";
            string extractPath = @"c:\users\exampleuser\extract";
            string newFile = @"c:\users\exampleuser\NewFile.txt";
            
            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(newFile, "NewEntry.txt");
                archive.ExtractToDirectory(extractPath);
            } 
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\users\exampleuser\end.zip"
        Dim extractPath As String = "c:\users\exampleuser\extract"
        Dim newFile As String = "c:\users\exampleuser\NewFile.txt"

        Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
            archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
            archive.ExtractToDirectory(extractPath)
        End Using
    End Sub

End Module

L’exemple suivant montre comment itérer au sein du contenu d’une archive zip et extraire les fichiers qui ont une extension. txt.The following example shows how to iterate through the contents of a zip archive and extract files that have a .txt extension.

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

Remarques

La ZipFileExtensions classe contient uniquement des méthodes statiques qui ZipArchive étendent les classes et ZipArchiveEntry .The ZipFileExtensions class contains only static methods that extend the ZipArchive and ZipArchiveEntry classes. Vous ne créez pas une instance de la ZipFileExtensions classe; à la place, vous utilisez ces méthodes à ZipArchive partir ZipArchiveEntryd’instances de ou de.You do not create an instance of the ZipFileExtensions class; instead, you use these methods from instances of ZipArchive or ZipArchiveEntry.

Pour utiliser les méthodes d’extension, vous devez référencer l' System.IO.Compression.FileSystem assembly dans votre projet.To use the extension methods, you must reference the System.IO.Compression.FileSystem assembly in your project. L' System.IO.Compression.FileSystem assembly n’est pas Windows 8.x StoreWindows 8.x Store disponible dans les applications.The System.IO.Compression.FileSystem assembly is not available in Windows 8.x StoreWindows 8.x Store apps. Par conséquent, ZipFileExtensions les ZipFile classes et (qui sont toutes les deux System.IO.Compression.FileSystem dans l’assembly) ne Windows 8.x StoreWindows 8.x Store sont pas disponibles dans les applications.Therefore, the ZipFileExtensions and ZipFile classes (both of which are in the System.IO.Compression.FileSystem assembly) are not available in Windows 8.x StoreWindows 8.x Store apps. Dans Windows 8.x StoreWindows 8.x Store les applications, vous travaillez avec des fichiers compressés à l' ZipArchiveEntryaide des méthodes GZipStreamde ZipArchive, DeflateStream, et.In Windows 8.x StoreWindows 8.x Store apps, you work with compressed files by using the methods in ZipArchive, ZipArchiveEntry, DeflateStream, and GZipStream.

La ZipFileExtensions classe contient quatre méthodes qui étendent ZipArchive:The ZipFileExtensions class contains four methods that extend ZipArchive:

La ZipFileExtensions classe contient deux méthodes qui étendent ZipArchiveEntryles éléments suivants:The ZipFileExtensions class contains two methods that extend ZipArchiveEntry:

Méthodes

CreateEntryFromFile(ZipArchive, String, String) CreateEntryFromFile(ZipArchive, String, String) CreateEntryFromFile(ZipArchive, String, String) CreateEntryFromFile(ZipArchive, String, String)

Archive un fichier en le compressant et en l'ajoutant à l'archive ZIP.Archives a file by compressing it and adding it to the zip archive.

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

Archive un fichier en le compressant à l'aide du niveau de compression spécifié et en l'ajoutant à l'archive ZIP.Archives a file by compressing it using the specified compression level and adding it to the zip archive.

ExtractToDirectory(ZipArchive, String) ExtractToDirectory(ZipArchive, String) ExtractToDirectory(ZipArchive, String) ExtractToDirectory(ZipArchive, String)

Extrait tous les fichiers dans l'archive zip d'un répertoire sur le système de fichiers.Extracts all the files in the zip archive to a directory on the file system.

ExtractToDirectory(ZipArchive, String, Boolean) ExtractToDirectory(ZipArchive, String, Boolean) ExtractToDirectory(ZipArchive, String, Boolean) ExtractToDirectory(ZipArchive, String, Boolean)

Extrait tous les fichiers de l’archive dans un répertoire du système de fichiers.Extracts all of the files in the archive to a directory on the file system.

ExtractToFile(ZipArchiveEntry, String) ExtractToFile(ZipArchiveEntry, String) ExtractToFile(ZipArchiveEntry, String) ExtractToFile(ZipArchiveEntry, String)

Extrait une entrée de l'archive zip dans un fichier.Extracts an entry in the zip archive to a file.

ExtractToFile(ZipArchiveEntry, String, Boolean) ExtractToFile(ZipArchiveEntry, String, Boolean) ExtractToFile(ZipArchiveEntry, String, Boolean) ExtractToFile(ZipArchiveEntry, String, Boolean)

Extrait une entrée dans l'archive zip d'un fichier et remplace éventuellement un fichier existant qui porte le même nom.Extracts an entry in the zip archive to a file, and optionally overwrites an existing file that has the same name.

S’applique à