Skip to content

Commit 51605f8

Browse files
committed
Back ZipEntry.DateTime with a real DateTime field instead of DosTime
Otherwise we lose the additional accuracy we gain from Unix or NT timestamps. Changing DateTime externally also changes DosTime. DosTime is only mapped to DateTime once on load, if there is no Unix or NT timestamp.
1 parent 6083154 commit 51605f8

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

ICSharpCode.SharpZipLib/Zip/ZipEntry.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ public ZipEntry(ZipEntry entry)
257257
compressedSize = entry.compressedSize;
258258
crc = entry.crc;
259259
dosTime = entry.dosTime;
260+
dateTime = entry.dateTime;
260261
method = entry.method;
261262
comment = entry.comment;
262263
versionToExtract = entry.versionToExtract;
@@ -706,16 +707,7 @@ public long DosTime
706707
/// </remarks>
707708
public DateTime DateTime
708709
{
709-
get
710-
{
711-
uint sec = Math.Min(59, 2 * (dosTime & 0x1f));
712-
uint min = Math.Min(59, (dosTime >> 5) & 0x3f);
713-
uint hrs = Math.Min(23, (dosTime >> 11) & 0x1f);
714-
uint mon = Math.Max(1, Math.Min(12, ((dosTime >> 21) & 0xf)));
715-
uint year = ((dosTime >> 25) & 0x7f) + 1980;
716-
int day = Math.Max(1, Math.Min(DateTime.DaysInMonth((int)year, (int)mon), (int)((dosTime >> 16) & 0x1f)));
717-
return new System.DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec);
718-
}
710+
get { return dateTime; }
719711

720712
set
721713
{
@@ -1058,7 +1050,7 @@ internal void ProcessExtraData(bool localHeader)
10581050
long lastAccess = extraData.ReadLong();
10591051
long createTime = extraData.ReadLong();
10601052

1061-
DateTime = System.DateTime.FromFileTimeUtc(lastModification);
1053+
dateTime = System.DateTime.FromFileTimeUtc(lastModification);
10621054
}
10631055
break;
10641056
} else {
@@ -1075,9 +1067,17 @@ internal void ProcessExtraData(bool localHeader)
10751067
if (((flags & 1) != 0) && (length >= 5)) {
10761068
int iTime = extraData.ReadInt();
10771069

1078-
DateTime = (new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc ) +
1079-
new TimeSpan(0, 0, 0, iTime, 0));
1070+
dateTime = (new System.DateTime ( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc ) +
1071+
new TimeSpan ( 0, 0, 0, iTime, 0 ));
10801072
}
1073+
} else {
1074+
uint sec = Math.Min(59, 2 * (dosTime & 0x1f));
1075+
uint min = Math.Min(59, (dosTime >> 5) & 0x3f);
1076+
uint hrs = Math.Min(23, (dosTime >> 11) & 0x1f);
1077+
uint mon = Math.Max(1, Math.Min(12, ((dosTime >> 21) & 0xf)));
1078+
uint year = ((dosTime >> 25) & 0x7f) + 1980;
1079+
int day = Math.Max(1, Math.Min(DateTime.DaysInMonth((int)year, (int)mon), (int)((dosTime >> 16) & 0x1f)));
1080+
dateTime = new DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec, DateTimeKind.Utc);
10811081
}
10821082
if (method == CompressionMethod.WinZipAES) {
10831083
ProcessAESExtraData(extraData);
@@ -1281,6 +1281,7 @@ public static string CleanName(string name)
12811281
ushort versionToExtract; // Version required to extract (library handles <= 2.0)
12821282
uint crc;
12831283
uint dosTime;
1284+
DateTime dateTime;
12841285

12851286
CompressionMethod method = CompressionMethod.Deflated;
12861287
byte[] extra;

0 commit comments

Comments
 (0)