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

Add support for old Info-ZIP extra block for Unix #660

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs
Expand Up @@ -955,6 +955,9 @@ internal void ProcessExtraData(bool localHeader)
ExtendedUnixData unixData = extraData.GetData<ExtendedUnixData>();
if (unixData != null && unixData.Include.HasFlag(ExtendedUnixData.Flags.ModificationTime))
return unixData.ModificationTime;
OldExtendedUnixData oldUnixData = extraData.GetData<OldExtendedUnixData>();
if (oldUnixData != null)
return oldUnixData.ModificationTime;

return null;
}
Expand Down
107 changes: 107 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs
Expand Up @@ -320,6 +320,113 @@ public Flags Include
#endregion Instance Fields
}

/// <summary>
/// Class representing old format for extended unix date time values.
/// </summary>
public class OldExtendedUnixData : ITaggedData
{
#region ITaggedData Members

/// <summary>
/// Get the ID
/// </summary>
public short TagID
{
get { return 0x5855; }
}

/// <summary>
/// Set the data from the raw values provided.
/// </summary>
/// <param name="data">The raw data to extract values from.</param>
/// <param name="index">The index to start extracting values from.</param>
/// <param name="count">The number of bytes available.</param>
public void SetData(byte[] data, int index, int count)
{
using (MemoryStream ms = new MemoryStream(data, index, count, false))
using (ZipHelperStream helperStream = new ZipHelperStream(ms))
{
int iTime = helperStream.ReadLEInt();

_modificationTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) +
new TimeSpan(0, 0, 0, iTime, 0);

iTime = helperStream.ReadLEInt();

_lastAccessTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) +
new TimeSpan(0, 0, 0, iTime, 0);
}
}

/// <summary>
/// Get the binary data representing this instance.
/// </summary>
/// <returns>The raw binary data representing this instance.</returns>
public byte[] GetData()
{
using (MemoryStream ms = new MemoryStream())
using (ZipHelperStream helperStream = new ZipHelperStream(ms))
{
helperStream.IsStreamOwner = false;

TimeSpan span = _modificationTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var seconds = (int)span.TotalSeconds;
helperStream.WriteLEInt(seconds);

span = _lastAccessTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
seconds = (int)span.TotalSeconds;
helperStream.WriteLEInt(seconds);

return ms.ToArray();
}
}

#endregion ITaggedData Members

/// <summary>
/// Get /set the Modification Time
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public DateTime ModificationTime
{
get { return _modificationTime; }
set
{
if (!ExtendedUnixData.IsValidValue(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}

_modificationTime = value;
}
}

/// <summary>
/// Get / set the Access Time
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public DateTime AccessTime
{
get { return _lastAccessTime; }
set
{
if (!ExtendedUnixData.IsValidValue(value))
{
throw new ArgumentOutOfRangeException(nameof(value));
}

_lastAccessTime = value;
}
}

#region Instance Fields

private DateTime _modificationTime = new DateTime(1970, 1, 1);
private DateTime _lastAccessTime = new DateTime(1970, 1, 1);

#endregion Instance Fields
}

/// <summary>
/// Class handling NT date time values.
/// </summary>
Expand Down