ZipFileExtensions Klasse

Definition

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
Vererbung
ZipFileExtensions

Beispiele

Das folgende Beispiel zeigt, wie Sie einen neuen Eintrag in einem ZIP-Archiv aus einer vorhandenen Datei erstellen und den Inhalt des Archivs in ein Verzeichnis extrahieren.

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

Das folgende Beispiel zeigt, wie Sie den Inhalt eines ZIP-Archivs durchlaufen und Dateien extrahieren, die über eine .txt Erweiterung verfügen.

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

Hinweise

Die ZipFileExtensions Klasse enthält vier Methoden, die erweitert werden ZipArchive:

Die ZipFileExtensions Klasse enthält zwei Methoden, die erweitert werden ZipArchiveEntry:

Methoden

Name Beschreibung
CreateEntryFromFile(ZipArchive, String, String, CompressionLevel, ReadOnlySpan<Char>, ZipEncryptionMethod)

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

Archiviert eine Datei, indem sie mithilfe der angegebenen Komprimierungsebene komprimiert und dem ZIP-Archiv hinzugefügt wird.

CreateEntryFromFile(ZipArchive, String, String, ReadOnlySpan<Char>, ZipEncryptionMethod)

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

CreateEntryFromFile(ZipArchive, String, String)

Archiviert eine Datei, indem sie komprimiert und zum ZIP-Archiv hinzugefügt wird.

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

Fügt dem Archiv unter dem angegebenen Eintragsnamen asynchron eine Datei aus dem Dateisystem hinzu.

CreateEntryFromFileAsync(ZipArchive, String, String, CompressionLevel, CancellationToken)

Fügt dem Archiv unter dem angegebenen Eintragsnamen asynchron eine Datei aus dem Dateisystem hinzu.

CreateEntryFromFileAsync(ZipArchive, String, String, CompressionLevel, ReadOnlyMemory<Char>, ZipEncryptionMethod, CancellationToken)

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

CreateEntryFromFileAsync(ZipArchive, String, String, ReadOnlyMemory<Char>, ZipEncryptionMethod, CancellationToken)

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

ExtractToDirectory(ZipArchive, String, Boolean)

Extrahiert alle Dateien im Archiv in ein Verzeichnis im Dateisystem.

ExtractToDirectory(ZipArchive, String, ZipExtractionOptions)

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

ExtractToDirectory(ZipArchive, String)

Extrahiert alle Dateien im ZIP-Archiv in ein Verzeichnis im Dateisystem.

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

Extrahiert alle Dateien im Archiv in ein Verzeichnis im Dateisystem. Das angegebene Verzeichnis ist möglicherweise bereits vorhanden.

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

Extrahiert asynchron alle Dateien im Archiv in ein Verzeichnis im Dateisystem. Das angegebene Verzeichnis kann bereits vorhanden sein.

ExtractToDirectoryAsync(ZipArchive, String, ZipExtractionOptions, CancellationToken)

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Extrahiert einen Eintrag im ZIP-Archiv in eine Datei und überschreibt optional eine vorhandene Datei mit demselben Namen.

ExtractToFile(ZipArchiveEntry, String, ZipExtractionOptions)

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

ExtractToFile(ZipArchiveEntry, String)

Extrahiert einen Eintrag im ZIP-Archiv in eine Datei.

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

Erstellt asynchron eine Datei im Dateisystem mit dem Inhalt des Eintrags und dem angegebenen Namen.

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

Erstellt asynchron eine Datei im Dateisystem mit dem Inhalt des Eintrags und dem angegebenen Namen.

ExtractToFileAsync(ZipArchiveEntry, String, ZipExtractionOptions, CancellationToken)

Stellt Erweiterungsmethoden für die ZipArchive und ZipArchiveEntry Klassen bereit.

Gilt für: