Skip to content

Commit

Permalink
Merge PR #433: Restore directory timestamps when extracting with FastZip
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Numpsy authored Jun 19, 2020
1 parent d506c55 commit 23841d2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/FastZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
57 changes: 57 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

/// <summary>
/// #426 - set the modified date for created directory entries if the RestoreDateTimeOnExtract option is enabled
/// </summary>
[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);
}
}
}
}

0 comments on commit 23841d2

Please sign in to comment.