RFC 1952 2.3.1 节指定 GZIP header 必须包含 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 。
为什么这个值是硬编码的?
最佳答案
正如 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 中修复。
关于java - 为什么 GZIP "os" header 在 Java 中被硬编码为 FAT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61715912/