2010-12-15 62 views
0

我使用Sharpziplib版本0.86.0來提取zip文件。它工作正常,但提取的文件是當前日期時間。我怎樣才能得到最初的DateTime?將LastWriteTime設置爲解壓縮文件的原始時間戳

public static void UnzipFile(string sourcePath, string targetDirectory) 
{ 
    try 
    { 
    using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourcePath))) 
    { 
     ZipEntry theEntry; 
     while ((theEntry = s.GetNextEntry()) != null) 
     { 
     //string directoryName = Path.GetDirectoryName(theEntry.Name); 
     string fileName = Path.GetFileName(theEntry.Name); 

     if (targetDirectory.Length > 0) 
     { 
      Directory.CreateDirectory(targetDirectory); 
     } 

     if (fileName != String.Empty) 
     { 
      using (FileStream streamWriter = File.Create(targetDirectory + fileName)) 
      { 
      int size = 2048; 
      byte[] data = new byte[2048]; 
      while (true) 
      { 
       size = s.Read(data, 0, data.Length); 
       if (size > 0) 
       { 
       streamWriter.Write(data, 0, size); 

       } 
       else 
       { 
       break; 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
    catch (Exception ex) 
    { 
    throw new Exception("Error unzipping file \"" + sourcePath + "\"", ex); 
    } 
} 
+0

有趣的大括號位置... – configurator 2010-12-15 06:21:10

回答

2

每個ZipEntry應該有一個包含文件的最後修改日期的時間戳DateTime屬性。

嘗試使用此值與File.SetLastWriteTime

+0

@configurator:我在哪裏以及如何給這個File.SetLastWriteTime ...你可以編輯我的代碼 – bala3569 2010-12-15 06:40:59

+0

寫完之後關閉'streamWriter'。我相信你可以從這裏弄清楚。 – configurator 2010-12-15 06:44:45

+0

@configurator:File.SetLastWriteTime(sourcePath,new DateTime());這是對的嗎? – bala3569 2010-12-15 06:48:41