Merge pull request #3037 from mono/system_io_compression_datetime
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Fri, 27 May 2016 18:20:18 +0000 (20:20 +0200)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Fri, 27 May 2016 18:20:18 +0000 (20:20 +0200)
[System.IO.Compression/.Filesystem] Fixed handling of date/time for Zip entries

mcs/class/System.IO.Compression.FileSystem/Test/System.IO.Compression.FileSystem/ZipTest.cs
mcs/class/System.IO.Compression.FileSystem/ZipFileExtensions.cs
mcs/class/System.IO.Compression/SharpCompress/Archive/Zip/ZipWritableArchiveEntry.cs
mcs/class/System.IO.Compression/Test/System.IO.Compression/ZipTest.cs

index 0354311b478478a834c847ac9c3a44f88cfb2887..4321db8534e30833c490da4c67df46455d1b29cb 100644 (file)
@@ -105,5 +105,31 @@ namespace MonoTests.System.IO.Compression.FileSystem
 
                        Directory.Delete ("extract", true);
                }
+
+               [Test]
+               public void ZipCreateFromEntryChangeTimestamp()
+               {
+                       if (File.Exists ("foo.zip"))
+                               File.Delete ("foo.zip");
+
+                       var file = "foo/foo.txt";
+                       using (var archive = new ZipArchive(File.Open("foo.zip", FileMode.Create),
+                               ZipArchiveMode.Update))
+                       {
+                               archive.CreateEntryFromFile(file, file);
+                       }
+
+                       var date = File.GetLastWriteTimeUtc(file);
+
+                       using (var archive = new ZipArchive (File.Open ("foo.zip", FileMode.Open),
+                               ZipArchiveMode.Read))
+                       {
+                               var entry = archive.GetEntry (file);
+                               Assert.IsNotNull (entry);
+                               Assert.AreEqual(entry.LastWriteTime.Year, date.Year);
+                               Assert.AreEqual(entry.LastWriteTime.Month, date.Month);
+                               Assert.AreEqual(entry.LastWriteTime.Day, date.Day);
+                       }
+               }
        }
 }
index 07a8448171ed51a1723c1d989ee53ddd071de7f2..36c4146b485139d4725bb0918abe8cf65ef919d8 100644 (file)
@@ -56,6 +56,7 @@ namespace System.IO.Compression
                                FileAccess.Read, FileShare.Read))
                        {
                                var zipArchiveEntry = destination.CreateEntry (entryName, compressionLevel);
+                               zipArchiveEntry.LastWriteTime = File.GetLastWriteTimeUtc(sourceFileName);
 
                                using (Stream entryStream = zipArchiveEntry.Open ())
                                        stream.CopyTo (entryStream);
index acb5e2ddc293f37845aa6dd4235dedef3175c7a1..f19a9c27647921fb0c277f4b8f5edfe8e8ffa7fc 100644 (file)
@@ -10,7 +10,6 @@ namespace SharpCompress.Archive.Zip
     {
         private readonly string path;
         private readonly long size;
-        private readonly DateTime? lastModified;
         private readonly bool closeStream;
         private readonly Stream stream;
         private bool isDisposed;
@@ -22,7 +21,7 @@ namespace SharpCompress.Archive.Zip
             this.stream = stream;
             this.path = path;
             this.size = size;
-            this.lastModified = lastModified;
+            this.LastModifiedTime = lastModified;
             this.closeStream = closeStream;
         }
 
@@ -46,11 +45,6 @@ namespace SharpCompress.Archive.Zip
             get { return size; }
         }
 
-        public override DateTime? LastModifiedTime
-        {
-            get { return lastModified; }
-        }
-
         public override DateTime? CreatedTime
         {
             get { return null; }
index 36903c675b4858391d77cc4354732ce4a12a8501..d461600299ffbbb1ee8e883897fd10787bd22ff9 100644 (file)
@@ -238,6 +238,31 @@ namespace MonoTests.System.IO.Compression
                        File.Delete ("create.zip");
                }
 
+               [Test]
+               public void ZipEnumerateEntriesModifiedTime()
+               {
+                       File.Copy("archive.zip", "test.zip", overwrite: true);
+                       var date = DateTimeOffset.Now;
+                       using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
+                               ZipArchiveMode.Update))
+                       {
+                               var entry = archive.GetEntry("foo.txt");
+                               entry.LastWriteTime = date;
+                       }
+
+                       using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
+                               ZipArchiveMode.Read))
+                       {
+                               var entry = archive.GetEntry("foo.txt");
+                               Assert.AreEqual(entry.LastWriteTime.Year, date.Year);
+                               Assert.AreEqual(entry.LastWriteTime.Month, date.Month);
+                               Assert.AreEqual(entry.LastWriteTime.Day, date.Day);
+
+                       }
+
+                       File.Delete ("test.zip");
+               }               
+
                [Test]
                public void ZipEnumerateEntriesReadMode()
                {