开发者

Setting LastWriteTime to an unzipped file's original timestamp

开发者 https://www.devze.com 2023-01-31 04:31 出处:网络
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?

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 unzippi开发者_运维百科ng file \"" + sourcePath + "\"", ex);
  }
}


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

Try using this value with File.SetLastWriteTime.

0

精彩评论

暂无评论...
验证码 换一张
取 消