Ler en inglés Editar

Compartir por


ZipArchive.GetEntry(String) Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Retrieves a wrapper for the specified entry in the zip archive.

C#
public System.IO.Compression.ZipArchiveEntry GetEntry(string entryName);
C#
public System.IO.Compression.ZipArchiveEntry? GetEntry(string entryName);

Parameters

entryName
String

A path, relative to the root of the archive, that identifies the entry to retrieve.

Returns

A wrapper for the specified entry in the archive; null if the entry does not exist in the archive.

Exceptions

entryName is Empty.

entryName is null.

The zip archive does not support reading.

The zip archive has been disposed.

The zip archive is corrupt, and its entries cannot be retrieved.

Examples

The following example shows how to use the GetEntry method to retrieve an entry.

C#
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;
            }
        }
    }
}

Remarks

If multiple entries that have the specified name exist in the archive, the first one is returned. The name of the entry is compared to entryName using ordinal comparison.

Applies to

Produto Versións
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0