ZipFileExtensions Sınıf

Tanım

ve ZipArchive sınıfları için ZipArchiveEntry uzantı yöntemleri sağlar.

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

Açıklamalar

ZipFileExtensions sınıfı yalnızca ve ZipArchive sınıflarını genişleten ZipArchiveEntry statik yöntemler içerir. Sınıfının bir örneğini ZipFileExtensions oluşturmazsınız; bunun yerine veya ZipArchiveörneklerinden ZipArchiveEntry bu yöntemleri kullanırsınız.

Uzantı yöntemlerini kullanmak için projenizdeki derlemeye System.IO.Compression.FileSystem başvurmanız gerekir. System.IO.Compression.FileSystem derlemesi Windows 8.x Store uygulamalarında kullanılamaz. Bu nedenle, ZipFileExtensions ve ZipFile sınıfları (her ikisi de System.IO.Compression.FileSystem derlemesindedir) Windows 8.x Store uygulamalarında kullanılamaz. Windows 8.x Store uygulamalarında ZipArchive, ZipArchiveEntry, DeflateStream ve GZipStream yöntemlerini kullanarak sıkıştırılmış dosyalarla çalışırsınız.

sınıfı, ZipFileExtensions öğesini genişleten ZipArchivedört yöntem içerir:

sınıfı, ZipFileExtensions öğesini genişleten ZipArchiveEntryiki yöntem içerir:

Örnekler

Aşağıdaki örnekte, var olan bir dosyadan zip arşivinde yeni bir girdi oluşturma ve arşivin içeriğini dizine ayıklama gösterilmektedir.

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

Aşağıdaki örnekte zip arşivinin içeriğinde yineleme ve .txt uzantısına sahip dosyaları ayıklama gösterilmektedir.

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

Yöntemler

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

Belirtilen sıkıştırma düzeyini kullanarak sıkıştırıp zip arşivine ekleyerek dosyayı arşivler.

CreateEntryFromFile(ZipArchive, String, String)

Dosyayı sıkıştırıp zip arşivine ekleyerek arşivler.

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

Dosya sisteminden belirtilen giriş adı altındaki arşive zaman uyumsuz olarak bir dosya ekler.

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

Dosya sisteminden belirtilen giriş adı altındaki arşive zaman uyumsuz olarak bir dosya ekler.

ExtractToDirectory(ZipArchive, String, Boolean)

Arşivdeki tüm dosyaları dosya sistemindeki bir dizine ayıklar.

ExtractToDirectory(ZipArchive, String)

Zip arşivindeki tüm dosyaları dosya sistemindeki bir dizine ayıklar.

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

Arşivdeki tüm dosyaları dosya sistemindeki bir dizine ayıklar. Belirtilen dizin zaten var olabilir.

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

Arşivdeki tüm dosyaları zaman uyumsuz olarak dosya sistemindeki bir dizine ayıklar. Belirtilen dizin zaten var olabilir.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Zip arşivindeki bir girdiyi bir dosyaya ayıklar ve isteğe bağlı olarak aynı ada sahip mevcut bir dosyanın üzerine yazar.

ExtractToFile(ZipArchiveEntry, String)

Zip arşivindeki bir girdiyi bir dosyaya ayıklar.

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

Zaman uyumsuz olarak, girdinin içeriği ve belirtilen adla dosya sisteminde bir dosya oluşturur.

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

Zaman uyumsuz olarak, girdinin içeriği ve belirtilen adla dosya sisteminde bir dosya oluşturur.

Şunlara uygulanır