Bewerken

Share via


ZipArchiveEntry.LastWriteTime Property

Definition

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

public:
 property DateTimeOffset LastWriteTime { DateTimeOffset get(); void set(DateTimeOffset value); };
public DateTimeOffset LastWriteTime { get; set; }
member this.LastWriteTime : DateTimeOffset with get, set
Public Property LastWriteTime As DateTimeOffset

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.

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

Applies to