Skip to content

Commit f40e24e

Browse files
committed
1 parent 20d87aa commit f40e24e

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

src/ICSharpCode.SharpZipLib/Zip/FastZip.cs

+90-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.IO;
55
using static ICSharpCode.SharpZipLib.Zip.Compression.Deflater;
6+
using static ICSharpCode.SharpZipLib.Zip.ZipEntryFactory;
67

78
namespace ICSharpCode.SharpZipLib.Zip
89
{
@@ -188,6 +189,29 @@ public enum Overwrite
188189

189190
#region Constructors
190191

192+
/// <summary>
193+
/// Initialise a new instance of <see cref="FastZip"/> using the specified <see cref="TimeSetting"/>
194+
/// </summary>
195+
/// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating or extracting <see cref="ZipEntry">Zip entries</see>.</param>
196+
/// <remarks>Using <see cref="TimeSetting.LastAccessTime">TimeSetting.LastAccessTime</see><see cref="TimeSetting.LastAccessTimeUtc">[Utc]</see> when
197+
/// creating an archive will set the file time to the moment of reading.
198+
/// </remarks>
199+
public FastZip(TimeSetting timeSetting)
200+
{
201+
entryFactory_ = new ZipEntryFactory(timeSetting);
202+
restoreDateTimeOnExtract_ = true;
203+
}
204+
205+
/// <summary>
206+
/// Initialise a new instance of <see cref="FastZip"/> using the specified <see cref="DateTime"/>
207+
/// </summary>
208+
/// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values for created or extracted <see cref="ZipEntry">Zip Entries</see>.</param>
209+
public FastZip(DateTime time)
210+
{
211+
entryFactory_ = new ZipEntryFactory(time);
212+
restoreDateTimeOnExtract_ = true;
213+
}
214+
191215
/// <summary>
192216
/// Initialise a default instance of <see cref="FastZip"/>.
193217
/// </summary>
@@ -733,7 +757,39 @@ private void ExtractFileEntry(ZipEntry entry, string targetName)
733757

734758
if (restoreDateTimeOnExtract_)
735759
{
736-
File.SetLastWriteTime(targetName, entry.DateTime);
760+
switch (entryFactory_.Setting)
761+
{
762+
case TimeSetting.CreateTime:
763+
File.SetCreationTime(targetName, entry.DateTime);
764+
break;
765+
766+
case TimeSetting.CreateTimeUtc:
767+
File.SetCreationTimeUtc(targetName, entry.DateTime);
768+
break;
769+
770+
case TimeSetting.LastAccessTime:
771+
File.SetLastAccessTime(targetName, entry.DateTime);
772+
break;
773+
774+
case TimeSetting.LastAccessTimeUtc:
775+
File.SetLastAccessTimeUtc(targetName, entry.DateTime);
776+
break;
777+
778+
case TimeSetting.LastWriteTime:
779+
File.SetLastWriteTime(targetName, entry.DateTime);
780+
break;
781+
782+
case TimeSetting.LastWriteTimeUtc:
783+
File.SetLastWriteTimeUtc(targetName, entry.DateTime);
784+
break;
785+
786+
case TimeSetting.Fixed:
787+
File.SetLastWriteTime(targetName, entryFactory_.FixedDateTime);
788+
break;
789+
790+
default:
791+
throw new ZipException("Unhandled time setting in ExtractFileEntry");
792+
}
737793
}
738794

739795
if (RestoreAttributesOnExtract && entry.IsDOSEntry && (entry.ExternalFileAttributes != -1))
@@ -807,7 +863,39 @@ private void ExtractEntry(ZipEntry entry)
807863
Directory.CreateDirectory(dirName);
808864
if (entry.IsDirectory && restoreDateTimeOnExtract_)
809865
{
810-
Directory.SetLastWriteTime(dirName, entry.DateTime);
866+
switch (entryFactory_.Setting)
867+
{
868+
case TimeSetting.CreateTime:
869+
Directory.SetCreationTime(dirName, entry.DateTime);
870+
break;
871+
872+
case TimeSetting.CreateTimeUtc:
873+
Directory.SetCreationTimeUtc(dirName, entry.DateTime);
874+
break;
875+
876+
case TimeSetting.LastAccessTime:
877+
Directory.SetLastAccessTime(dirName, entry.DateTime);
878+
break;
879+
880+
case TimeSetting.LastAccessTimeUtc:
881+
Directory.SetLastAccessTimeUtc(dirName, entry.DateTime);
882+
break;
883+
884+
case TimeSetting.LastWriteTime:
885+
Directory.SetLastWriteTime(dirName, entry.DateTime);
886+
break;
887+
888+
case TimeSetting.LastWriteTimeUtc:
889+
Directory.SetLastWriteTimeUtc(dirName, entry.DateTime);
890+
break;
891+
892+
case TimeSetting.Fixed:
893+
Directory.SetLastWriteTime(dirName, entryFactory_.FixedDateTime);
894+
break;
895+
896+
default:
897+
throw new ZipException("Unhandled time setting in ExtractEntry");
898+
}
811899
}
812900
}
813901
else

src/ICSharpCode.SharpZipLib/Zip/IEntryFactory.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
using System;
2+
13
using ICSharpCode.SharpZipLib.Core;
24

5+
using static ICSharpCode.SharpZipLib.Zip.ZipEntryFactory;
6+
37
namespace ICSharpCode.SharpZipLib.Zip
48
{
59
/// <summary>
@@ -50,5 +54,16 @@ public interface IEntryFactory
5054
/// Get/set the <see cref="INameTransform"></see> applicable.
5155
/// </summary>
5256
INameTransform NameTransform { get; set; }
57+
58+
/// <summary>
59+
/// Get the <see cref="TimeSetting"/> in use.
60+
/// </summary>
61+
TimeSetting Setting { get; }
62+
63+
/// <summary>
64+
/// Get the <see cref="DateTime"/> value to use when <see cref="Setting"/> is set to <see cref="TimeSetting.Fixed"/>,
65+
/// or if not specified, the value of <see cref="DateTime.Now"/> when the class was the initialized
66+
/// </summary>
67+
DateTime FixedDateTime { get; }
5368
}
5469
}

src/ICSharpCode.SharpZipLib/Zip/ZipEntryFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)
322322

323323
private INameTransform nameTransform_;
324324
private DateTime fixedDateTime_ = DateTime.Now;
325-
private TimeSetting timeSetting_;
325+
private TimeSetting timeSetting_ = TimeSetting.LastWriteTime;
326326
private bool isUnicodeText_;
327327

328328
private int getAttributes_ = -1;

0 commit comments

Comments
 (0)