Skip to content

Commit cef5453

Browse files
committed
feat: add support for old Info-ZIP extra block for Unix (Info-ZIP Unix Extra Field type 1)
1 parent cd5310f commit cef5453

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,9 @@ internal void ProcessExtraData(bool localHeader)
955955
ExtendedUnixData unixData = extraData.GetData<ExtendedUnixData>();
956956
if (unixData != null && unixData.Include.HasFlag(ExtendedUnixData.Flags.ModificationTime))
957957
return unixData.ModificationTime;
958+
OldExtendedUnixData oldUnixData = extraData.GetData<OldExtendedUnixData>();
959+
if (oldUnixData != null)
960+
return oldUnixData.ModificationTime;
958961

959962
return null;
960963
}

src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,113 @@ public Flags Include
320320
#endregion Instance Fields
321321
}
322322

323+
/// <summary>
324+
/// Class representing old format for extended unix date time values.
325+
/// </summary>
326+
public class OldExtendedUnixData : ITaggedData
327+
{
328+
#region ITaggedData Members
329+
330+
/// <summary>
331+
/// Get the ID
332+
/// </summary>
333+
public short TagID
334+
{
335+
get { return 0x5855; }
336+
}
337+
338+
/// <summary>
339+
/// Set the data from the raw values provided.
340+
/// </summary>
341+
/// <param name="data">The raw data to extract values from.</param>
342+
/// <param name="index">The index to start extracting values from.</param>
343+
/// <param name="count">The number of bytes available.</param>
344+
public void SetData(byte[] data, int index, int count)
345+
{
346+
using (MemoryStream ms = new MemoryStream(data, index, count, false))
347+
using (ZipHelperStream helperStream = new ZipHelperStream(ms))
348+
{
349+
int iTime = helperStream.ReadLEInt();
350+
351+
_modificationTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) +
352+
new TimeSpan(0, 0, 0, iTime, 0);
353+
354+
iTime = helperStream.ReadLEInt();
355+
356+
_lastAccessTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) +
357+
new TimeSpan(0, 0, 0, iTime, 0);
358+
}
359+
}
360+
361+
/// <summary>
362+
/// Get the binary data representing this instance.
363+
/// </summary>
364+
/// <returns>The raw binary data representing this instance.</returns>
365+
public byte[] GetData()
366+
{
367+
using (MemoryStream ms = new MemoryStream())
368+
using (ZipHelperStream helperStream = new ZipHelperStream(ms))
369+
{
370+
helperStream.IsStreamOwner = false;
371+
372+
TimeSpan span = _modificationTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
373+
var seconds = (int)span.TotalSeconds;
374+
helperStream.WriteLEInt(seconds);
375+
376+
span = _lastAccessTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
377+
seconds = (int)span.TotalSeconds;
378+
helperStream.WriteLEInt(seconds);
379+
380+
return ms.ToArray();
381+
}
382+
}
383+
384+
#endregion ITaggedData Members
385+
386+
/// <summary>
387+
/// Get /set the Modification Time
388+
/// </summary>
389+
/// <exception cref="ArgumentOutOfRangeException"></exception>
390+
public DateTime ModificationTime
391+
{
392+
get { return _modificationTime; }
393+
set
394+
{
395+
if (!ExtendedUnixData.IsValidValue(value))
396+
{
397+
throw new ArgumentOutOfRangeException(nameof(value));
398+
}
399+
400+
_modificationTime = value;
401+
}
402+
}
403+
404+
/// <summary>
405+
/// Get / set the Access Time
406+
/// </summary>
407+
/// <exception cref="ArgumentOutOfRangeException"></exception>
408+
public DateTime AccessTime
409+
{
410+
get { return _lastAccessTime; }
411+
set
412+
{
413+
if (!ExtendedUnixData.IsValidValue(value))
414+
{
415+
throw new ArgumentOutOfRangeException(nameof(value));
416+
}
417+
418+
_lastAccessTime = value;
419+
}
420+
}
421+
422+
#region Instance Fields
423+
424+
private DateTime _modificationTime = new DateTime(1970, 1, 1);
425+
private DateTime _lastAccessTime = new DateTime(1970, 1, 1);
426+
427+
#endregion Instance Fields
428+
}
429+
323430
/// <summary>
324431
/// Class handling NT date time values.
325432
/// </summary>

0 commit comments

Comments
 (0)