ZipArchiveEntry.LastWriteTime Property

Definition

Gets or sets the last time the entry in the zip archive was changed.

C#
public DateTimeOffset LastWriteTime { get; set; }

Property Value

The last time the entry in the zip archive was changed.

Exceptions

The attempt to set this property failed, because the zip archive for the entry is in Read mode.

The archive mode is set to Create.

-or-

The archive mode is set to Update and the entry has been opened.

An attempt was made to set this property to a value that is either earlier than 1980 January 1 0:00:00 (midnight) or later than 2107 December 31 23:59:58 (one second before midnight).

Remarks

When you create a new entry from an existing file by calling the CreateEntryFromFile method, the LastWriteTime property for the entry is automatically set to the last time the file was modified. When you create a new entry programmatically by calling the CreateEntry method, the LastWriteTime property for the entry is automatically set to the time of execution. If you modify the entry, you must explicitly set the LastWriteTime property if you want the value to reflect the time of the latest change.

When you set this property, the DateTimeOffset value is converted to a timestamp format that is specific to zip archives. This format supports a resolution of two seconds. The earliest permitted value is 1980 January 1 0:00:00 (midnight). The latest permitted value is 2107 December 31 23:59:58 (one second before midnight). If the value for the last write time is not valid, the property returns a default value of 1980 January 1 0:00:00 (midnight).

Examples

The following example shows how to open an entry in a zip archive, modify it, and set the LastWriteTime property to the current time.

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

Applies to

Product Versions
.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
.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