I'm trying to set the ZipArchiveEntry.LastWriteTime
so that when extracted, it is the 'exact' same time, but I'm getting random time differences. Here is a specific example.
var fileName = @"C:\BTR\Publish\ESS\Test\RTX\Global.asax";
var sourceRoot = @"C:\BTR\Publish\ESS\Test\RTX"; // Want entry full name to start with RTX
var target = @"C:\BTR\Publish\LWT.Test.zip";
var lwt = File.GetLastWriteTime(fileName);
Console.WriteLine( $"File: {fileName}, Date {lwt:yyyy-MM-dd HH:mm:ss.ff}" );
using ( var fs = File.Create( target ) )
using (var stream = new ZipArchive(fs, ZipArchiveMode.Create))
using (var source = File.OpenRead(fileName))
{
var rootLength = Path.GetDirectoryName(sourceRoot).Length;
var entryName = fileName.Substring(rootLength + 1);
var entry = stream.CreateEntry(entryName, CompressionLevel.Fastest);
entry.LastWriteTime = lwt;
Console.WriteLine( $"ZipArchiveEntry: {fileName}, File Date: {lwt:yyyy-MM-dd HH:mm:ss.ff}, Zip Date: {entry.LastWriteTime:yyyy-MM-dd HH:mm:ss.ff}" );
using (var es = entry.Open())
{
source.CopyTo(es);
}
}
DateTime zipLwt;
using (var fs = File.Open(target, FileMode.Open))
using (var zip = new ZipArchive(fs, ZipArchiveMode.Read))
{
zipLwt = zip.Entries[ 0 ].LastWriteTime.DateTime;
Console.WriteLine( $"Zip File Entry: {zip.Entries[0].FullName}: Date {zipLwt:yyyy-MM-dd HH:mm:ss.ff}" );
}
Console.WriteLine($"Date Difference: {(lwt - zipLwt).TotalMilliseconds}ms" );
This code outputs:
File: C:\BTR\Publish\ESS\Test\RTX\Global.asax, Date 2016-10-14 05:54:49.98
ZipArchiveEntry: C:\BTR\Publish\ESS\Test\RTX\Global.asax, File Date: 2016-10-14 05:54:49.98, Zip Date: 2016-10-14 05:54:49.98
Zip File Entry: RTX\Global.asax: Date 2016-10-14 05:54:48.00 Date
Difference: 1989.243ms
Any ideas on how to make the dates exact? When I set the LastWriteTime
it appears to be good according to the output, but when I extract it, it is wrong.