ZipArchiveEntry 类

定义

表示 zip 档案中的压缩文件。Represents a compressed file within a zip archive.

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

示例

第一个示例演示如何在 zip 存档中创建新项并对其进行写入。The first example shows how to create a new entry in a zip archive and write to it.

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) 扩展方法。The second example shows how to use the ExtractToFile(ZipArchiveEntry, String) extension method. 若要执行代码,必须在项目中引用 System.IO.Compression.FileSystem 程序集。You must reference the System.IO.Compression.FileSystem assembly in your project for the code to execute.

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

注解

Zip 存档包含每个压缩文件的条目。A zip archive contains an entry for each compressed file. 使用 ZipArchiveEntry 类可以检查项的属性,然后打开或删除该项。The ZipArchiveEntry class enables you to examine the properties of an entry, and open or delete the entry. 打开某个条目时,可以通过写入该压缩文件的流来修改压缩的文件。When you open an entry, you can modify the compressed file by writing to the stream for that compressed file.

操作 zip 存档的方法和它们的文件条目分布在三个类中: ZipFileZipArchiveZipArchiveEntryThe methods for manipulating zip archives and their file entries are spread across three classes: ZipFile, ZipArchive and ZipArchiveEntry.

自...To… 使用...Use…
从目录创建 zip 存档Create a zip archive from a directory ZipFile.CreateFromDirectory
将 zip 存档的内容提取到目录Extract the contents of a zip archive to a directory ZipFile.ExtractToDirectory
将新文件添加到现有 zip 存档Add new files to an existing zip archive ZipArchive.CreateEntry
在 zip 存档中检索文件Retrieve an file in a zip archive ZipArchive.GetEntry
检索 zip 存档中的所有文件Retrieve all of the files in a zip archive ZipArchive.Entries
打开 zip 存档中包含的单个文件的流To open a stream to an individual file contained in a zip archive ZipArchiveEntry.Open
删除 zip 存档中的文件Delete a file from a zip archive ZipArchiveEntry.Delete

如果在项目中引用 System.IO.Compression.FileSystem 程序集,则可以访问 ZipArchiveEntry 类的两个扩展方法。If you reference the System.IO.Compression.FileSystem assembly in your project, you can access two extension methods for the ZipArchiveEntry class. 这些方法是 ExtractToFile(ZipArchiveEntry, String)ExtractToFile(ZipArchiveEntry, String, Boolean)的,它们使你能够将项的内容解压缩到文件中。Those methods are ExtractToFile(ZipArchiveEntry, String) and ExtractToFile(ZipArchiveEntry, String, Boolean), and they enable you to decompress the contents of the entry to a file. System.IO.Compression.FileSystem 程序集在 Windows 8Windows 8中不可用。The System.IO.Compression.FileSystem assembly is not available in Windows 8Windows 8. Windows 8.x 应用商店Windows 8.x Store 应用中,可以通过使用 DeflateStreamGZipStream解压缩存档的内容,也可以使用 Windows 运行时Windows Runtime 类型压缩程序和解压缩程序压缩和解压缩文件。In Windows 8.x 应用商店Windows 8.x Store apps, you can decompress the contents of an archive by using DeflateStream or GZipStream, or you can use the Windows 运行时Windows Runtime types Compressor and Decompressor to compress and decompress files.

属性

Archive

获取该项所属的 zip 存档。Gets the zip archive that the entry belongs to.

CompressedLength

获取在 zip 存档中的项的压缩大小。Gets the compressed size of the entry in the zip archive.

Crc32

32 位循环冗余检查。The 32-bit Cyclic Redundant Check.

ExternalAttributes

操作系统和应用程序特定的文件属性。OS and application specific file attributes.

FullName

获取 zip 存档中的项的相对路径。Gets the relative path of the entry in the zip archive.

LastWriteTime

获取或设置最近一次更改 zip 存档中的项的时间。Gets or sets the last time the entry in the zip archive was changed.

Length

获取 zip 存档中的项的未压缩大小。Gets the uncompressed size of the entry in the zip archive.

Name

获取在 zip 存档中的项的文件名。Gets the file name of the entry in the zip archive.

方法

Delete()

删除 zip 存档中的项。Deletes the entry from the zip archive.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Open()

打开 zip 存档中的项。Opens the entry from the zip archive.

ToString()

检索 zip 存档中项的相对路径。Retrieves the relative path of the entry in the zip archive.

扩展方法

ExtractToFile(ZipArchiveEntry, String)

将 zip 存档中的条目解压到文件下。Extracts an entry in the zip archive to a file.

ExtractToFile(ZipArchiveEntry, String, Boolean)

将 zip 存档中的条目解压缩到文件下,并可选择覆盖具有相同名称的现有文件。Extracts an entry in the zip archive to a file, and optionally overwrites an existing file that has the same name.

适用于