将LastWriteTime设置为解压缩文件的原始时间戳

时间:2010-12-15 06:17:51

标签: c# sharpziplib modified-date

我使用Sharpziplib版本0.86.0来提取zip文件。它工作正常,但提取的文件是当前的DateTime。我怎么能得到原始的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);
  }
}

1 个答案:

答案 0 :(得分:2)

每个ZipEntry都应该有一个DateTime属性,其中包含文件上次修改日期的时间戳。

尝试将此值与File.SetLastWriteTime一起使用。