Why do gzip of Java and Go get different results?

后端 未结 2 1078
自闭症患者
自闭症患者 2020-12-11 06:41

Firstly, my Java version:

string str = \"helloworld\";
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(str.length());
GZIPOutput         


        
2条回答
  •  生来不讨喜
    2020-12-11 07:16

    From RFC 1952, the GZip file header is structured as:

    +---+---+---+---+---+---+---+---+---+---+
    |ID1|ID2|CM |FLG|     MTIME     |XFL|OS | (more-->)
    +---+---+---+---+---+---+---+---+---+---+
    

    Looking at the output you've provided, we have:

                              |    Java |          Go
    ID1                       |      31 |          31
    ID2                       |     139 |         139
    CM (compression method)   |       8 |           8
    FLG (flags)               |       0 |           0
    MTIME (modification time) | 0 0 0 0 | 0 9 110 136
    XFL (extra flags)         |       0 |           0
    OS (operating system)     |       0 |         255
    

    So we can see that Go is setting the modification time field of the header, and setting the operating system to 255 (unknown) rather than 0 (FAT file system). In other respects they indicate that the file is compressed in the same way.

    In general these sorts of differences are harmless. If you want to determine if two compressed files are the same, then you should really compare the decompressed versions of the files though.

提交回复
热议问题