ZipFileExtensions Osztály

Definíció

Bővítménymetelyeket biztosít az osztályokhoz és ZipArchive az ZipArchiveEntry osztályokhoz.

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
Öröklődés
ZipFileExtensions

Megjegyzések

Az ZipFileExtensions osztály csak azokat a statikus metódusokat tartalmazza, amelyek kiterjesztik az osztályokat és ZipArchive az ZipArchiveEntry osztályokat. Nem hozza létre az osztály egy példányát; ehelyett ezeket a ZipFileExtensions metódusokat használja az osztály példányaiból vagy ZipArchivepéldányaibólZipArchiveEntry.

A bővítménymetelyek használatához hivatkoznia kell a System.IO.Compression.FileSystem projekt szerelvényére. A System.IO.Compression.FileSystem szerelvény nem érhető el a Windows 8.x Áruházbeli alkalmazásokban. Ezért a ZipFileExtensions és a ZipFile osztály (mindkettő a System.IO.Compression.FileSystem szerelvényben található) nem érhető el a Windows 8.x Áruházbeli alkalmazásokban. A Windows 8.x Áruházbeli alkalmazásokban tömörített fájlokkal dolgozhat a ZipArchive, ZipArchiveEntry, DeflateStream és GZipStream metódusokkal.

Az ZipFileExtensions osztály négy olyan metódust tartalmaz, amelyek kiterjednek ZipArchive:

Az ZipFileExtensions osztály két olyan metódust tartalmaz, amelyek kiterjednek ZipArchiveEntry:

Példák

Az alábbi példa bemutatja, hogyan hozhat létre új bejegyzést egy zip-archívumban egy meglévő fájlból, és hogyan nyerheti ki az archívum tartalmát egy könyvtárba.

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

Az alábbi példa bemutatja, hogyan lehet iterálni a zip archívum tartalmát, és kinyerni azokat a fájlokat, amelyek .txt kiterjesztéssel rendelkeznek.

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

Metódusok

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

A fájl archiválása a megadott tömörítési szinttel történő tömörítéssel és a zip archívumba való hozzáadásával.

CreateEntryFromFile(ZipArchive, String, String)

Egy fájl archiválása tömörítéssel és a zip archívumba való hozzáadásával.

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

Aszinkron módon hozzáad egy fájlt a fájlrendszerből az archívumhoz a megadott bejegyzésnév alatt.

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

Aszinkron módon hozzáad egy fájlt a fájlrendszerből az archívumhoz a megadott bejegyzésnév alatt.

ExtractToDirectory(ZipArchive, String, Boolean)

Kinyeri az archívumban lévő összes fájlt a fájlrendszer egyik könyvtárába.

ExtractToDirectory(ZipArchive, String)

Kinyeri a zip archívumban lévő összes fájlt a fájlrendszer egyik könyvtárába.

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

Kinyeri az archívumban lévő összes fájlt a fájlrendszer egyik könyvtárába. Előfordulhat, hogy a megadott könyvtár már létezik.

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

Aszinkron módon kinyeri az archívumban lévő összes fájlt a fájlrendszer egyik könyvtárába. A megadott könyvtár már létezhet.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Kinyer egy bejegyzést a zip-archívumból egy fájlba, és opcionálisan felülír egy azonos nevű meglévő fájlt.

ExtractToFile(ZipArchiveEntry, String)

Kinyer egy bejegyzést a zip archívumból egy fájlba.

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

Aszinkron módon létrehoz egy fájlt a fájlrendszeren a bejegyzés tartalmával és a megadott névvel.

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

Aszinkron módon létrehoz egy fájlt a fájlrendszeren a bejegyzés tartalmával és a megadott névvel.

A következőre érvényes: