0

我正在尝试设置,ZipArchiveEntry.LastWriteTime以便在提取时它是“精确”相同的时间,但我得到了随机的时差。这是一个具体的例子。

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" );

此代码输出:

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

关于如何使日期准确的任何想法?当我LastWriteTime根据输出设置它时,它似乎很好,但是当我提取它时,它是错误的。

4

0 回答 0