2

我刚刚开始在 .NET 4.5 中使用 System.IO.Compression,发现了一个问题。它使用本地修改时间而不是通用 UTC 时间存储文件。

因此,如果您在一个时区压缩文件并在另一个时区解压缩它们,它会使用原始文件中的本地修改时间(比如下午 1 点),并以相同的修改时间(也是下午 1 点)提取文件,即使它应该早或晚几个小时。

我假设在标准时间或夏令时压缩的文件也会存在同样的问题,然后在另一个时间解压缩。

似乎在压缩过程中缺少设置,因为其他解压缩方法(WinZip,压缩文件夹提取)会产生相同的错误修改时间。

我已经测试过使用WinZip在不同时区压缩和解压缩文件,它没有这个问题。它必须在内部使用 UTC 作为修改时间。

除了在 Zip 和 Unzip 期间建立我自己的时移例程之外,还有什么办法可以解决这个问题?

此项目不能使用任何外部应用程序或库。我们仅限于使用 .NET 函数。

4

1 回答 1

2

正如 Hans Passant 在评论中提到的,zip 文件格式使用MS-DOS 日期和时间结构。

该结构被定义为两个单独的unsigned short值,如下所示:

wFatDate

The MS-DOS date. The date is a packed value with the following format.
Bits    Description
0-4     Day of the month (1–31)
5-8     Month (1 = January, 2 = February, and so on)
9-15    Year offset from 1980 (add 1980 to get actual year)

wFatTime

The MS-DOS time. The time is a packed value with the following format.
Bits    Description
0-4     Second divided by 2
5-10    Minute (0–59)
11-15   Hour (0–23 on a 24-hour clock)

在创建 MS-DOS 时,这些计算机上还没有使用时区(不过,自 1970 年以来,Unix 就已经有了这个概念。)使用 MS-DOS 的人经常在办公室或家里,不与人交流在其他国家更不用说其他国家通过计算机。Intranet 当时也相当昂贵。

创建 zip 文件格式的公司犯了使用 FAT 文件系统日期格式的错误并且卡住了。所以 zip 文件是使用本地时间创建的(它不是必须的,但这是预期的行为,至少。)

不过,zip 格式提供了添加扩展的方法(来自发表评论的@user3342816 的链接),包括各种时间戳。

0x000a        NTFS (Win9x/WinNT FileTimes)
0x000d        Unix

NTFS 块的描述如下:

     -PKWARE Win95/WinNT Extra Field:
      ==============================

      The following description covers PKWARE's "NTFS" attributes
      "extra" block, introduced with the release of PKZIP 2.50 for
      Windows. (Last Revision 20001118)

      (Note: At this time the Mtime, Atime and Ctime values may
      be used on any WIN32 system.)
      [Info-ZIP note: In the current implementations, this field has
      a fixed total data size of 32 bytes and is only stored as local
      extra field.]

      Value         Size        Description
      -----         ----        -----------
      0x000a        Short       Tag (NTFS) for this "extra" block type
      TSize         Short       Total Data Size for this block
      Reserved      Long        for future use
      Tag1          Short       NTFS attribute tag value #1
      Size1         Short       Size of attribute #1, in bytes
      (var.)        SubSize1    Attribute #1 data
      .
      .
      .
      TagN          Short       NTFS attribute tag value #N
      SizeN         Short       Size of attribute #N, in bytes
      (var.)        SubSize1    Attribute #N data

      For NTFS, values for Tag1 through TagN are as follows:
      (currently only one set of attributes is defined for NTFS)

      Tag        Size       Description
      -----      ----       -----------
      0x0001     2 bytes    Tag for attribute #1
      Size1      2 bytes    Size of attribute #1, in bytes (24)
      Mtime      8 bytes    64-bit NTFS file last modification time
      Atime      8 bytes    64-bit NTFS file last access time
      Ctime      8 bytes    64-bit NTFS file creation time

      The total length for this block is 28 bytes, resulting in a
      fixed size value of 32 for the TSize field of the NTFS block.

      The NTFS filetimes are 64-bit unsigned integers, stored in Intel
      (least significant byte first) byte order. They determine the
      number of 1.0E-07 seconds (1/10th microseconds!) past WinNT "epoch",
      which is "01-Jan-1601 00:00:00 UTC".

Unix 块也包括两个时间戳:

     -PKWARE Unix Extra Field:
      ========================

      The following is the layout of PKWARE's Unix "extra" block.
      It was introduced with the release of PKZIP for Unix 2.50.
      Note: all fields are stored in Intel low-byte/high-byte order.
      (Last Revision 19980901)

      This field has a minimum data size of 12 bytes and is only stored
      as local extra field.

      Value         Size        Description
      -----         ----        -----------
      0x000d        Short       Tag (Unix0) for this "extra" block type
      TSize         Short       Total Data Size for this block
      AcTime        Long        time of last access (UTC/GMT)
      ModTime       Long        time of last modification (UTC/GMT)
      UID           Short       Unix user ID
      GID           Short       Unix group ID
      (var)         variable    Variable length data field

      The variable length data field will contain file type
      specific data.  Currently the only values allowed are
      the original "linked to" file names for hard or symbolic
      links, and the major and minor device node numbers for
      character and block device nodes.  Since device nodes
      cannot be either symbolic or hard links, only one set of
      variable length data is stored.  Link files will have the
      name of the original file stored.  This name is NOT NULL
      terminated.  Its size can be determined by checking TSize -
      12.  Device entries will have eight bytes stored as two 4
      byte entries (in little-endian format).  The first entry
      will be the major device number, and the second the minor
      device number.

      [Info-ZIP note: The fixed part of this field has the same layout as
      Info-ZIP's abandoned "Unix1 timestamps & owner ID info" extra field;
      only the two tag bytes are different.]

正如我们所见,NTFS 和 Unix 块清楚地将它们的时间戳定义为使用 UTC。NTFS 日期比 Unix 时间戳 (1s) 具有更高的精度 (100ms),由于它使用 64 位,它也将存活更长的时间(有关 32 位时间戳的更多详细信息,请参阅2038 年问题)。

于 2019-04-08T07:43:56.403 回答