ZipFileExtensions 類別

定義

提供 ZipArchiveZipArchiveEntry 類別的擴充方法。

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
繼承
ZipFileExtensions

備註

類別 ZipFileExtensions 只包含擴充 和 ZipArchiveEntry 類別的 ZipArchive 靜態方法。 您不會建立 類別的 ZipFileExtensions 實例,而是使用 或 ZipArchiveEntry 實例 ZipArchive 中的這些方法。

若要使用擴充方法,您必須參考專案中的 System.IO.Compression.FileSystem 元件。 元件 System.IO.Compression.FileSystem 無法在 Windows 8.x Store 應用程式中使用。 因此, ZipFileExtensions Windows 8.x Store 應用程式中無法使用元件) 中的 System.IO.Compression.FileSystemZipFile 類別 (。 在 Windows 8.x Store 應用程式中,您可以使用 、 ZipArchiveEntryDeflateStreamGZipStreamZipArchive 的方法來處理壓縮檔。

類別 ZipFileExtensions 包含四個擴充 ZipArchive 的方法:

類別 ZipFileExtensions 包含兩個擴充 ZipArchiveEntry 的方法:

範例

下列範例示範如何從現有的檔案在 zip 封存中建立新專案,並將封存的內容解壓縮到目錄。

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

下列範例示範如何逐一查看 zip 封存的內容,以及擷取副檔名為.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

方法

CreateEntryFromFile(ZipArchive, String, String)

透過將檔案壓縮並加入至 zip 封存的方式進行封存。

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

透過將檔案以指定之壓縮等級壓縮並加入至 zip 封存的方式進行封存。

ExtractToDirectory(ZipArchive, String)

將 zip 封存中的所有檔案都解壓縮到檔案系統上的目錄。

ExtractToDirectory(ZipArchive, String, Boolean)

將封存中的所有檔案解壓縮到檔案系統上目錄。

ExtractToFile(ZipArchiveEntry, String)

將 zip 封存中的項目解壓縮至檔案。

ExtractToFile(ZipArchiveEntry, String, Boolean)

擷取檔案 zip 封存中的項目,並選擇性地覆寫具有相同名稱的現有檔案。

適用於