Skip to content

Commit 23841d2

Browse files
authored
Merge PR #433: Restore directory timestamps when extracting with FastZip
* When extracting folders with FastZip, reset the last modified time if the RestoreDateTimeOnExtract option is enabled * Add unit test for restoring directory timestamps when extracing with fastzip and RestoreDateTimeOnExtract is true
1 parent d506c55 commit 23841d2

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/ICSharpCode.SharpZipLib/Zip/FastZip.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,11 @@ private void ExtractEntry(ZipEntry entry)
708708
try
709709
{
710710
Directory.CreateDirectory(dirName);
711+
712+
if (entry.IsDirectory && restoreDateTimeOnExtract_)
713+
{
714+
Directory.SetLastWriteTime(dirName, entry.DateTime);
715+
}
711716
}
712717
catch (Exception ex)
713718
{

test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,5 +568,62 @@ public void StreamClosedOnError()
568568
// test folder should not have been created on error
569569
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should still not exist");
570570
}
571+
572+
/// <summary>
573+
/// #426 - set the modified date for created directory entries if the RestoreDateTimeOnExtract option is enabled
574+
/// </summary>
575+
[Test]
576+
[Category("Zip")]
577+
[Category("CreatesTempFile")]
578+
public void SetDirectoryModifiedDate()
579+
{
580+
string tempFilePath = GetTempFilePath();
581+
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
582+
583+
string zipName = Path.Combine(tempFilePath, $"{nameof(SetDirectoryModifiedDate)}.zip");
584+
585+
EnsureTestDirectoryIsEmpty(tempFilePath);
586+
587+
var modifiedTime = new DateTime(2001, 1, 2);
588+
string targetDir = Path.Combine(tempFilePath, ZipTempDir, nameof(SetDirectoryModifiedDate));
589+
using (FileStream fs = File.Create(zipName))
590+
{
591+
using (ZipOutputStream zOut = new ZipOutputStream(fs))
592+
{
593+
// Add an empty directory entry, with a specified time field
594+
var entry = new ZipEntry("emptyFolder/")
595+
{
596+
DateTime = modifiedTime
597+
};
598+
zOut.PutNextEntry(entry);
599+
}
600+
}
601+
602+
try
603+
{
604+
// extract the zip
605+
var fastZip = new FastZip
606+
{
607+
CreateEmptyDirectories = true,
608+
RestoreDateTimeOnExtract = true
609+
};
610+
fastZip.ExtractZip(zipName, targetDir, "zz");
611+
612+
File.Delete(zipName);
613+
614+
// Check that the empty sub folder exists and has the expected modlfied date
615+
string emptyTargetDir = Path.Combine(targetDir, "emptyFolder");
616+
617+
Assert.That(Directory.Exists(emptyTargetDir), Is.True, "Empty directory should be created");
618+
619+
var extractedFolderTime = Directory.GetLastWriteTime(emptyTargetDir);
620+
Assert.That(extractedFolderTime, Is.EqualTo(modifiedTime));
621+
}
622+
finally
623+
{
624+
// Tidy up
625+
Directory.Delete(targetDir, true);
626+
}
627+
}
571628
}
572629
}

0 commit comments

Comments
 (0)