ZipArchiveEntry 类

定义

表示 zip 档案中的压缩文件。

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

注解

zip 存档包含每个压缩文件的条目。 类 ZipArchiveEntry 使你能够检查条目的属性,并打开或删除该条目。 打开条目时,可以通过写入该压缩文件的流来修改压缩文件。

用于操作 zip 存档及其文件条目的方法分布在三个类中: ZipFileZipArchiveZipArchiveEntry

收件人... 使用...
从目录Create 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 应用商店应用中,可以使用 或 GZipStream解压缩存档DeflateStream的内容,也可以使用 Windows 运行时 类型和 CompressorDecompressor 压缩和解压缩文件。

示例

第一个示例演示如何在 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

操作系统和应用程序特定的文件属性。

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 存档中的条目解压缩到文件下,并可选择覆盖具有相同名称的现有文件。

适用于