为什么 GZIP "os" header hard-coded 到 Java 中的 FAT?

Why is the GZIP "os" header hard-coded to FAT in Java?

RFC 1952 部分 2.3.1 指定 GZIP headers 必须包含一个 OS 标志:

OS (Operating System). This identifies the type of file system on which compression took place. This may be useful in determining end-of-line convention for text files. The currently defined values are as follows:

  0 - FAT filesystem (MS-DOS, OS/2, NT/Win32)
  1 - Amiga
  2 - VMS (or OpenVMS)
  3 - Unix
  4 - VM/CMS
  5 - Atari TOS
  6 - HPFS filesystem (OS/2, NT)
  7 - Macintosh
  8 - Z-System
  9 - CP/M
 10 - TOPS-20
 11 - NTFS filesystem (NT)
 12 - QDOS
 13 - Acorn RISCOS
255 - unknown

然而,Java 的 GZIP 序列化在所有情况下都会写入零,如 line 193 of GzipOutputStream.java 中所示。我已经 运行 在四种不同的操作系统上进行了测试,以确认没有其他代码在编写后修改此 header。

为什么这个值是hard-coded?

正如 Elliott 所指出的,根据您引用的同一 RFC 的第 2.3.1.2 节,将其设置为默认值是可以的:

A compliant compressor must produce files with correct ID1, ID2, CM, CRC32, and ISIZE, but may set all the other fields in the fixed-length part of the header to default values (255 for OS, 0 for all others). The compressor must set all reserved bits to zero.

但是,根据这个片段,默认值仍然不正确 - OS 标志的默认值是 255,而不是 0。这是一个 已知错误 根据 JDK-8244706 在 JDK 中。它已在 Java 版本 16,抢先体验版本 16 中修复。