ZipArchiveEntry 類別

定義

表示 zip 封存中的壓縮檔。

public ref class ZipArchiveEntry
public class ZipArchiveEntry
type ZipArchiveEntry = class
Public Class ZipArchiveEntry
繼承
ZipArchiveEntry

備註

zip 封存包含每個壓縮檔的專案。 類別 ZipArchiveEntry 可讓您檢查項目的屬性,並開啟或刪除專案。 當您開啟專案時,您可以藉由寫入該壓縮檔的資料流程來修改壓縮檔案。

用來操作 zip 封存及其檔案專案的方法會分散到三個類別: ZipFileZipArchiveZipArchiveEntry

收件人... 請使用...
從目錄建立 ZIP 封存 ZipFile.CreateFromDirectory
將 ZIP 封存的內容解壓縮到目錄 ZipFile.ExtractToDirectory
將新檔案新增至現有的 zip 封存 ZipArchive.CreateEntry
擷取 zip 封存中的檔案 ZipArchive.GetEntry
擷取 zip 封存中的所有檔案 ZipArchive.Entries
開啟 zip 封存中包含的個別檔案串流 ZipArchiveEntry.Open
從 zip 封存中刪除檔案 ZipArchiveEntry.Delete

如果您參考專案中的 System.IO.Compression.FileSystem 元件,您可以存取 類別的 ZipArchiveEntry 兩個擴充方法。 這些方法是 ExtractToFile(ZipArchiveEntry, String)ExtractToFile(ZipArchiveEntry, String, Boolean) ,而且可讓您將專案的內容解壓縮至檔案。 元件 System.IO.Compression.FileSystem 無法在 Windows 8中使用。 在 Windows 8.x Store 應用程式中,您可以使用 或 GZipStream 來解壓縮封存 DeflateStream 的內容,或者您可以使用 Windows 執行階段 類型 Compressor ,以及 Decompressor 壓縮和解壓縮檔案。

範例

第一個範例示範如何在 zip 封存中建立新的專案,並寫入它。

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine("Information about this package.");
                            writer.WriteLine("========================");
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Using zipToOpen As FileStream = New FileStream("c:\users\exampleuser\release.zip", FileMode.Open)
            Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
                Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("Readme.txt")
                Using writer As StreamWriter = New StreamWriter(readmeEntry.Open())
                    writer.WriteLine("Information about this package.")
                    writer.WriteLine("========================")
                End Using
            End Using
        End Using
    End Sub

End Module

第二個範例示範如何使用 ExtractToFile(ZipArchiveEntry, String) 擴充方法。 您必須參考 System.IO.Compression.FileSystem 專案中的元件,才能執行程式碼。

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

屬性

Archive

取得項目所屬的 zip 封存。

Comment

取得或設定選擇性專案批註。

CompressedLength

取得 Zip 封存中的項目壓縮大小。

Crc32

32 位元循環冗餘檢查。

ExternalAttributes

OS 和應用程式專用的檔案屬性。

FullName

取得 Zip 封存中的項目相對路徑。

IsEncrypted

取得值,這個值表示是否加密專案。

LastWriteTime

取得或設定上次變更 zip 封存中之項目的時間。

Length

取得 Zip 封存中的項目未壓縮大小。

Name

取得 Zip 封存中的項目檔名。

方法

Delete()

從 zip 封存中刪除項目。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Open()

從 zip 封存中開啟項目。

ToString()

擷取 Zip 封存中項目的相對路徑。

擴充方法

ExtractToFile(ZipArchiveEntry, String)

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

ExtractToFile(ZipArchiveEntry, String, Boolean)

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

適用於