0

I am using Sharpziplib version 0.86.0 to extract a zip file. It is working fine but the files extracted are with current DateTime. How could I get the original DateTime?

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);
  }
}
bala3569
  • 10,832
  • 28
  • 102
  • 146

1 Answers1

2

Each ZipEntry should have a DateTime property containing the timestamp of the file's last modification date.

Try using this value with File.SetLastWriteTime.

configurator
  • 40,828
  • 14
  • 81
  • 115
  • @configurator:where and how i have to give this File.SetLastWriteTime...can u edit my code – bala3569 Dec 15 '10 at 06:40
  • After you've finished writing to it and closed `streamWriter`. I'm sure you can figure it out from here. – configurator Dec 15 '10 at 06:44
  • @configurator:File.SetLastWriteTime(sourcePath, new DateTime()); Is it correct? – bala3569 Dec 15 '10 at 06:48
  • @bala: That will set the zip file to have the current time. Is this what you want? – configurator Dec 15 '10 at 06:57
  • @configurator:No i need original timestamp of that file...so instead of new DateTime() i have to give theEntry.DateTime?? – bala3569 Dec 15 '10 at 06:59
  • "Each ZipEntry should have a DateTime property containing the timestamp of the file's last modification date." – configurator Dec 15 '10 at 07:01
  • You've got a ZipEntry. It's called `theEntry`. Your output file is called `targetDirectory + fileName`. Now put all three things together and you've got `File.SetLastWriteTime(targetDirectory + fileName, theEntry.DateTime)` – configurator Dec 15 '10 at 07:08
  • Actual file Lastwritetime:4/8/2010 2:29:03PM – bala3569 Dec 15 '10 at 08:48
  • After zipping that file using winzip and while extracting that file using the code,actual files Lastwritetime changes to 4/8/2010 2:29:04PM – bala3569 Dec 15 '10 at 08:49
  • 1
    Presumably you would also want to set the CreationTime and LastAccessedTime properties as well? – David Keaveny Jan 21 '11 at 03:04