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

发布于 2024-10-07 20:15:33 字数 1266 浏览 0 评论 0原文

我正在使用 Sharpziplib 版本 0.86.0 来提取 zip 文件。它工作正常,但提取的文件具有当前日期时间。我怎样才能得到原始的日期时间?

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);
  }
}

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);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-10-14 20:15:33

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

尝试将此值与 File.SetLastWriteTime

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

Try using this value with File.SetLastWriteTime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文