Skip to content

Commit

Permalink
Merge pull request #877 from StarkDirewolf/master
Browse files Browse the repository at this point in the history
Fixed bug in zip time header flags
  • Loading branch information
adamhathcock authored Nov 1, 2024
2 parents 6aeef8d + 0faa176 commit ceddab9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ public UnixTimeExtraField(ExtraDataType type, ushort length, byte[] dataBytes)
return Tuple.Create<DateTime?, DateTime?, DateTime?>(null, null, null);
}

var flags = DataBytes[0];
var isModifiedTimeSpecified = (flags & 0x01) == 1;
var isLastAccessTimeSpecified = (flags & 0x02) == 1;
var isCreationTimeSpecified = (flags & 0x04) == 1;
var flags = (RecordedTimeFlag)DataBytes[0];
var isModifiedTimeSpecified = flags.HasFlag(RecordedTimeFlag.LastModified);
var isLastAccessTimeSpecified = flags.HasFlag(RecordedTimeFlag.LastAccessed);
var isCreationTimeSpecified = flags.HasFlag(RecordedTimeFlag.Created);
var currentIndex = 1;
DateTime? modifiedTime = null;
DateTime? lastAccessTime = null;
Expand All @@ -189,7 +189,7 @@ public UnixTimeExtraField(ExtraDataType type, ushort length, byte[] dataBytes)
{
if (currentIndex + 4 > DataBytes.Length)
{
throw new ArchiveException("Invalid UnicodeExtraTime field");
return Tuple.Create<DateTime?, DateTime?, DateTime?>(null, null, null);
}

var lastAccessEpochTime = BinaryPrimitives.ReadInt32LittleEndian(
Expand All @@ -206,7 +206,7 @@ public UnixTimeExtraField(ExtraDataType type, ushort length, byte[] dataBytes)
{
if (currentIndex + 4 > DataBytes.Length)
{
throw new ArchiveException("Invalid UnicodeExtraTime field");
return Tuple.Create<DateTime?, DateTime?, DateTime?>(null, null, null);
}

var creationTimeEpochTime = BinaryPrimitives.ReadInt32LittleEndian(
Expand All @@ -222,6 +222,15 @@ public UnixTimeExtraField(ExtraDataType type, ushort length, byte[] dataBytes)
return Tuple.Create(modifiedTime, lastAccessTime, creationTime);
}
}

[Flags]
private enum RecordedTimeFlag
{
None = 0,
LastModified = 1,
LastAccessed = 2,
Created = 4
}
}

internal static class LocalEntryHeaderExtraFactory
Expand Down
24 changes: 21 additions & 3 deletions src/SharpCompress/Common/Zip/ZipEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SharpCompress.Common.Zip.Headers;

namespace SharpCompress.Common.Zip;
Expand All @@ -15,10 +16,19 @@ internal ZipEntry(ZipFilePart? filePart)
return;
}
_filePart = filePart;

LastModifiedTime = Utility.DosDateToDateTime(
filePart.Header.LastModifiedDate,
filePart.Header.LastModifiedTime
);

var times =
filePart.Header.Extra.FirstOrDefault(header =>
header.GetType() == typeof(UnixTimeExtraField)
) as UnixTimeExtraField;

LastAccessedTime = times?.UnicodeTimes.Item2;
CreatedTime = times?.UnicodeTimes.Item3;
}

public override CompressionType CompressionType =>
Expand Down Expand Up @@ -51,9 +61,17 @@ internal ZipEntry(ZipFilePart? filePart)

public override DateTime? LastModifiedTime { get; }

public override DateTime? CreatedTime => null;

public override DateTime? LastAccessedTime => null;
/// <inheritdoc/>
/// <remarks>
/// The returned time is UTC, not local.
/// </remarks>
public override DateTime? CreatedTime { get; }

/// <inheritdoc/>
/// <remarks>
/// The returned time is UTC, not local.
/// </remarks>
public override DateTime? LastAccessedTime { get; }

public override DateTime? ArchivedTime => null;

Expand Down

0 comments on commit ceddab9

Please sign in to comment.