ZipArchive.GetEntry(String) 方法
定义
在 zip 存档中检索指定项的包装。Retrieves a wrapper for the specified entry in the zip archive.
public:
System::IO::Compression::ZipArchiveEntry ^ GetEntry(System::String ^ entryName);
public System.IO.Compression.ZipArchiveEntry GetEntry (string entryName);
public System.IO.Compression.ZipArchiveEntry? GetEntry (string entryName);
member this.GetEntry : string -> System.IO.Compression.ZipArchiveEntry
Public Function GetEntry (entryName As String) As ZipArchiveEntry
参数
- entryName
- String
相对于存档的根的路径标识了项目检索。A path, relative to the root of the archive, that identifies the entry to retrieve.
返回
存档中指定项的包装器;如果存档中不存在此项,则为 null。A wrapper for the specified entry in the archive; null if the entry does not exist in the archive.
例外
entryName 为 null。entryName is null.
zip 存档不支持读取。The zip archive does not support reading.
zip 存档已释放。The zip archive has been disposed.
zip 存档已损坏,不能检索其项。The zip archive is corrupt, and its entries cannot be retrieved.
示例
下面的示例演示如何使用 GetEntry 方法检索项。The following example shows how to use the GetEntry method to retrieve an entry.
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string zipPath = @"c:\example\result.zip";
using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
{
ZipArchiveEntry entry = archive.GetEntry("ExistingFile.txt");
using (StreamWriter writer = new StreamWriter(entry.Open()))
{
writer.BaseStream.Seek(0, SeekOrigin.End);
writer.WriteLine("append line to file");
}
entry.LastWriteTime = DateTimeOffset.UtcNow.LocalDateTime;
}
}
}
}
Imports System.IO
Imports System.IO.Compression
Module Module1
Sub Main()
Dim zipPath As String = "c:\example\result.zip"
Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
Dim entry As ZipArchiveEntry = archive.GetEntry("ExistingFile.txt")
Using writer As StreamWriter = New StreamWriter(entry.Open())
writer.BaseStream.Seek(0, SeekOrigin.End)
writer.WriteLine("append line to file")
End Using
entry.LastWriteTime = DateTimeOffset.UtcNow.LocalDateTime
End Using
End Sub
End Module
注解
如果存档中存在多个具有指定名称的条目,则返回第一个条目。If multiple entries that have the specified name exist in the archive, the first one is returned. 使用序号比较比较项的名称 entryName 。The name of the entry is compared to entryName using ordinal comparison.