ZipFileExtensions Klas

Definitie

Biedt uitbreidingsmethoden voor de ZipArchive en ZipArchiveEntry klassen.

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

Opmerkingen

De ZipFileExtensions klasse bevat alleen statische methoden waarmee de ZipArchive en ZipArchiveEntry klassen worden uitgebreid. U maakt geen exemplaar van de ZipFileExtensions klasse. In plaats daarvan gebruikt u deze methoden van exemplaren of ZipArchiveZipArchiveEntry.

Als u de extensiemethoden wilt gebruiken, moet u verwijzen naar de System.IO.Compression.FileSystem assembly in uw project. De assembly System.IO.Compression.FileSystem is niet beschikbaar in Windows 8.x Store-apps. Daarom zijn de klassen ZipFileExtensions en ZipFile (beide in de assembly System.IO.Compression.FileSystem) niet beschikbaar in Windows 8.x Store-apps. In Windows 8.x Store-apps werkt u met gecomprimeerde bestanden met behulp van de methoden in ZipArchive, ZipArchiveEntry, DeflateStream en GZipStream.

De ZipFileExtensions klasse bevat vier methoden die het volgende uitbreiden ZipArchive:

De ZipFileExtensions klasse bevat twee methoden die het volgende uitbreiden ZipArchiveEntry:

Voorbeelden

In het volgende voorbeeld ziet u hoe u een nieuwe vermelding maakt in een zip-archief van een bestaand bestand en hoe u de inhoud van het archief kunt extraheren naar een map.

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

In het volgende voorbeeld ziet u hoe u de inhoud van een zip-archief kunt herhalen en bestanden kunt extraheren met een .txt extensie.

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

Methoden

Name Description
CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

Hiermee wordt een bestand gearchiveerd door het te comprimeren met behulp van het opgegeven compressieniveau en het toe te voegen aan het zip-archief.

CreateEntryFromFile(ZipArchive, String, String)

Hiermee wordt een bestand gearchiveerd door het te comprimeren en toe te voegen aan het zip-archief.

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

Voegt asynchroon een bestand uit het bestandssysteem toe aan het archief onder de opgegeven vermeldingsnaam.

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

Voegt asynchroon een bestand uit het bestandssysteem toe aan het archief onder de opgegeven vermeldingsnaam.

ExtractToDirectory(ZipArchive, String, Boolean)

Hiermee worden alle bestanden in het archief geëxtraheerd naar een map in het bestandssysteem.

ExtractToDirectory(ZipArchive, String)

Extraheert alle bestanden in het zip-archief naar een map in het bestandssysteem.

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

Hiermee worden alle bestanden in het archief geëxtraheerd naar een map in het bestandssysteem. De opgegeven map bestaat mogelijk al.

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

Alle bestanden in het archief worden asynchroon geëxtraheerd naar een map in het bestandssysteem. De opgegeven map kan al bestaan.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Extraheert een vermelding in het zip-archief naar een bestand en overschrijft eventueel een bestaand bestand met dezelfde naam.

ExtractToFile(ZipArchiveEntry, String)

Extraheert een vermelding in het zip-archief naar een bestand.

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

Asynchroon maakt u een bestand op het bestandssysteem met de inhoud van het item en de opgegeven naam.

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

Asynchroon maakt u een bestand op het bestandssysteem met de inhoud van het item en de opgegeven naam.

Van toepassing op