Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug in zip time header flags #877

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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