From 23841d246d52480344a37f93d5fad7f3ceaa1c69 Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Fri, 19 Jun 2020 22:21:27 +0100 Subject: [PATCH] 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 --- src/ICSharpCode.SharpZipLib/Zip/FastZip.cs | 5 ++ .../Zip/FastZipHandling.cs | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/ICSharpCode.SharpZipLib/Zip/FastZip.cs b/src/ICSharpCode.SharpZipLib/Zip/FastZip.cs index 319645a5b..2e1719e88 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/FastZip.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/FastZip.cs @@ -708,6 +708,11 @@ private void ExtractEntry(ZipEntry entry) try { Directory.CreateDirectory(dirName); + + if (entry.IsDirectory && restoreDateTimeOnExtract_) + { + Directory.SetLastWriteTime(dirName, entry.DateTime); + } } catch (Exception ex) { diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs index b1a0eb100..67b481f65 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs @@ -568,5 +568,62 @@ public void StreamClosedOnError() // test folder should not have been created on error Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should still not exist"); } + + /// + /// #426 - set the modified date for created directory entries if the RestoreDateTimeOnExtract option is enabled + /// + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + public void SetDirectoryModifiedDate() + { + string tempFilePath = GetTempFilePath(); + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); + + string zipName = Path.Combine(tempFilePath, $"{nameof(SetDirectoryModifiedDate)}.zip"); + + EnsureTestDirectoryIsEmpty(tempFilePath); + + var modifiedTime = new DateTime(2001, 1, 2); + string targetDir = Path.Combine(tempFilePath, ZipTempDir, nameof(SetDirectoryModifiedDate)); + using (FileStream fs = File.Create(zipName)) + { + using (ZipOutputStream zOut = new ZipOutputStream(fs)) + { + // Add an empty directory entry, with a specified time field + var entry = new ZipEntry("emptyFolder/") + { + DateTime = modifiedTime + }; + zOut.PutNextEntry(entry); + } + } + + try + { + // extract the zip + var fastZip = new FastZip + { + CreateEmptyDirectories = true, + RestoreDateTimeOnExtract = true + }; + fastZip.ExtractZip(zipName, targetDir, "zz"); + + File.Delete(zipName); + + // Check that the empty sub folder exists and has the expected modlfied date + string emptyTargetDir = Path.Combine(targetDir, "emptyFolder"); + + Assert.That(Directory.Exists(emptyTargetDir), Is.True, "Empty directory should be created"); + + var extractedFolderTime = Directory.GetLastWriteTime(emptyTargetDir); + Assert.That(extractedFolderTime, Is.EqualTo(modifiedTime)); + } + finally + { + // Tidy up + Directory.Delete(targetDir, true); + } + } } }