ZipFileExtensions Třída

Definice

Poskytuje rozšiřující metody pro třídy ZipArchive a ZipArchiveEntry třídy.

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
Dědičnost
ZipFileExtensions

Poznámky

Třída ZipFileExtensions obsahuje pouze statické metody, které rozšiřují třídyZipArchive.ZipArchiveEntry Nevytvoříte instanci ZipFileExtensions třídy; místo toho použijete tyto metody z instancí ZipArchive nebo ZipArchiveEntry.

Chcete-li použít rozšiřující metody, musíte odkazovat na System.IO.Compression.FileSystem sestavení v projektu. Sestavení System.IO.Compression.FileSystem není dostupné v aplikacích pro Windows 8.x Store. Proto třídy ZipFileExtensions a ZipFile (obě třídy jsou v sestavení System.IO.Compression.FileSystem) nejsou dostupné v aplikacích pro Windows 8.x Store. V aplikacích Windows 8.x Store pracujete s komprimovanými soubory pomocí metod v ZipArchive, ZipArchiveEntry, DeflateStream a GZipStream.

Třída ZipFileExtensions obsahuje čtyři metody, které rozšiřují ZipArchive:

Třída ZipFileExtensions obsahuje dvě metody, které rozšiřují ZipArchiveEntry:

Příklady

Následující příklad ukazuje, jak vytvořit novou položku v archivu zip z existujícího souboru a extrahovat obsah archivu do adresáře.

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

Následující příklad ukazuje, jak iterovat obsah archivu zip a extrahovat soubory, které mají příponu .txt.

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

Metody

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

Archivuje soubor tak, že ho zkomprimuje pomocí zadané úrovně komprese a přidá ho do archivu zip.

CreateEntryFromFile(ZipArchive, String, String)

Archivuje soubor tím, že ho zkomprimuje a přidá do archivu zip.

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

Asynchronně přidá soubor ze systému souborů do archivu pod zadaný název položky.

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

Asynchronně přidá soubor ze systému souborů do archivu pod zadaný název položky.

ExtractToDirectory(ZipArchive, String, Boolean)

Extrahuje všechny soubory v archivu do adresáře v systému souborů.

ExtractToDirectory(ZipArchive, String)

Extrahuje všechny soubory v archivu zip do adresáře v systému souborů.

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

Extrahuje všechny soubory v archivu do adresáře v systému souborů. Zadaný adresář již může existovat.

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

Asynchronně extrahuje všechny soubory v archivu do adresáře v systému souborů. Zadaný adresář již může existovat.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Extrahuje položku v archivu zip do souboru a volitelně přepíše existující soubor se stejným názvem.

ExtractToFile(ZipArchiveEntry, String)

Extrahuje položku v archivu zip do souboru.

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

Asynchronně vytvoří soubor v systému souborů s obsahem položky a zadaným názvem.

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

Asynchronně vytvoří soubor v systému souborů s obsahem položky a zadaným názvem.

Platí pro