如何在 Java 中获取 gzip 内文件的时间戳



我想保留从 Java 中的 gzip 文件中提取的文件的时间戳。

代码如下:

   public void gunzipFile(String zipFile, String newFile) {
    System.out.println("zipFile: " + zipFile);
    final int bufferSize = 1024;
    try {
        FileInputStream fis = new FileInputStream(zipFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        GZIPInputStream gis = new GZIPInputStream(bis);
        FileOutputStream fos = new FileOutputStream(newFile);
        final byte[] buffer = new byte[bufferSize];
        int len = 0;
        while ((len = gis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        //close resources
        fos.close();
        gis.close();
    } catch (IOException e) {
        System.out.println("exception caught");
    }
}
这是一个

笨拙的解决方案,因为GZIPInputStream类无法为您提供时间戳。

FileInputStream fis = new FileInputStream(zipFile);
byte[] header = new byte[10];
fis.read(header);
int timestamp = header[4] & 0xFF |
            (header[5] & 0xFF) << 8 |
            (header[6] & 0xFF) << 16 |
            (header[7] & 0xFF) << 24; 
// or more simply, use
// int timestamp = ByteBuffer.wrap(header, 4, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();
System.out.println(new Date((long) timestamp * 1000)); // this will give you the date

GZIP 格式对某些元数据使用 10 字节标头。字节 5(偏移量 4)到 8 表示 unix 时间戳。如果将它们转换为int并乘以 1000 得到毫秒,则可以获取文件中的日期(如果最初有一个)。

格式为

    0        1
+--------+--------+
|00001000|00000010|
+--------+--------+
 ^        ^
 |        |
 |        + more significant byte = 2 x 256
 + less significant byte = 8

换句话说,第一个字节是int的最后 8 位。这就是LITTLE_ENDIAN的用武之地。

我建议您在这里小心使用InputStream。可能使用 BufferedInputStreamreset() 来定位0或只是打开不同的InputStream。使用一个获取时间戳,并使用另一个膨胀 gzip 内容。

最新更新