Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
HowToDoThis committed Aug 11, 2021
1 parent 20d87aa commit f40e24e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
92 changes: 90 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/FastZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using static ICSharpCode.SharpZipLib.Zip.Compression.Deflater;
using static ICSharpCode.SharpZipLib.Zip.ZipEntryFactory;

namespace ICSharpCode.SharpZipLib.Zip
{
Expand Down Expand Up @@ -188,6 +189,29 @@ public enum Overwrite

#region Constructors

/// <summary>
/// Initialise a new instance of <see cref="FastZip"/> using the specified <see cref="TimeSetting"/>
/// </summary>
/// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating or extracting <see cref="ZipEntry">Zip entries</see>.</param>
/// <remarks>Using <see cref="TimeSetting.LastAccessTime">TimeSetting.LastAccessTime</see><see cref="TimeSetting.LastAccessTimeUtc">[Utc]</see> when
/// creating an archive will set the file time to the moment of reading.
/// </remarks>
public FastZip(TimeSetting timeSetting)
{
entryFactory_ = new ZipEntryFactory(timeSetting);
restoreDateTimeOnExtract_ = true;
}

/// <summary>
/// Initialise a new instance of <see cref="FastZip"/> using the specified <see cref="DateTime"/>
/// </summary>
/// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values for created or extracted <see cref="ZipEntry">Zip Entries</see>.</param>
public FastZip(DateTime time)
{
entryFactory_ = new ZipEntryFactory(time);
restoreDateTimeOnExtract_ = true;
}

/// <summary>
/// Initialise a default instance of <see cref="FastZip"/>.
/// </summary>
Expand Down Expand Up @@ -733,7 +757,39 @@ private void ExtractFileEntry(ZipEntry entry, string targetName)

if (restoreDateTimeOnExtract_)
{
File.SetLastWriteTime(targetName, entry.DateTime);
switch (entryFactory_.Setting)
{
case TimeSetting.CreateTime:
File.SetCreationTime(targetName, entry.DateTime);
break;

case TimeSetting.CreateTimeUtc:
File.SetCreationTimeUtc(targetName, entry.DateTime);
break;

case TimeSetting.LastAccessTime:
File.SetLastAccessTime(targetName, entry.DateTime);
break;

case TimeSetting.LastAccessTimeUtc:
File.SetLastAccessTimeUtc(targetName, entry.DateTime);
break;

case TimeSetting.LastWriteTime:
File.SetLastWriteTime(targetName, entry.DateTime);
break;

case TimeSetting.LastWriteTimeUtc:
File.SetLastWriteTimeUtc(targetName, entry.DateTime);
break;

case TimeSetting.Fixed:
File.SetLastWriteTime(targetName, entryFactory_.FixedDateTime);
break;

default:
throw new ZipException("Unhandled time setting in ExtractFileEntry");
}
}

if (RestoreAttributesOnExtract && entry.IsDOSEntry && (entry.ExternalFileAttributes != -1))
Expand Down Expand Up @@ -807,7 +863,39 @@ private void ExtractEntry(ZipEntry entry)
Directory.CreateDirectory(dirName);
if (entry.IsDirectory && restoreDateTimeOnExtract_)
{
Directory.SetLastWriteTime(dirName, entry.DateTime);
switch (entryFactory_.Setting)
{
case TimeSetting.CreateTime:
Directory.SetCreationTime(dirName, entry.DateTime);
break;

case TimeSetting.CreateTimeUtc:
Directory.SetCreationTimeUtc(dirName, entry.DateTime);
break;

case TimeSetting.LastAccessTime:
Directory.SetLastAccessTime(dirName, entry.DateTime);
break;

case TimeSetting.LastAccessTimeUtc:
Directory.SetLastAccessTimeUtc(dirName, entry.DateTime);
break;

case TimeSetting.LastWriteTime:
Directory.SetLastWriteTime(dirName, entry.DateTime);
break;

case TimeSetting.LastWriteTimeUtc:
Directory.SetLastWriteTimeUtc(dirName, entry.DateTime);
break;

case TimeSetting.Fixed:
Directory.SetLastWriteTime(dirName, entryFactory_.FixedDateTime);
break;

default:
throw new ZipException("Unhandled time setting in ExtractEntry");
}
}
}
else
Expand Down
15 changes: 15 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/IEntryFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;

using ICSharpCode.SharpZipLib.Core;

using static ICSharpCode.SharpZipLib.Zip.ZipEntryFactory;

namespace ICSharpCode.SharpZipLib.Zip
{
/// <summary>
Expand Down Expand Up @@ -50,5 +54,16 @@ public interface IEntryFactory
/// Get/set the <see cref="INameTransform"></see> applicable.
/// </summary>
INameTransform NameTransform { get; set; }

/// <summary>
/// Get the <see cref="TimeSetting"/> in use.
/// </summary>
TimeSetting Setting { get; }

/// <summary>
/// Get the <see cref="DateTime"/> value to use when <see cref="Setting"/> is set to <see cref="TimeSetting.Fixed"/>,
/// or if not specified, the value of <see cref="DateTime.Now"/> when the class was the initialized
/// </summary>
DateTime FixedDateTime { get; }
}
}
2 changes: 1 addition & 1 deletion src/ICSharpCode.SharpZipLib/Zip/ZipEntryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)

private INameTransform nameTransform_;
private DateTime fixedDateTime_ = DateTime.Now;
private TimeSetting timeSetting_;
private TimeSetting timeSetting_ = TimeSetting.LastWriteTime;
private bool isUnicodeText_;

private int getAttributes_ = -1;
Expand Down

0 comments on commit f40e24e

Please sign in to comment.