Is it possible to check if a file (.gz) has been compressed more than once?

I was presented with a situation where a file with a patented format was compressed in .gz, and then renamed to the original extension and then compressed again. I would like to fix such a scenario and wonder if there is a way to detect when the file was compressed twice.

I read the .gz files as follows:

 GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(inFile));
 BufferedReader breader = new BufferedReader(new InputStreamReader(gzip)); 
+4
source share
2 answers

You can check the valid gzip header in the file. The gzip file must contain a specific header starting with a 2-byte number with the values ​​0x1f and 0x8b (see spec ). You can check these bytes to see if they match the header values:

InputStream is = new FileInputStream(new File(filePath));
byte[] b = new byte[2];
int n = is.read(b);
if ( n != 2 ){
    //not a gzip file
}
if ( (b[0] == (byte) 0x1f) && (b[1] == (byte)0x8b)){
    //2-byte gzip header
}

1/65k, , , , . , , , (. - , , 8 DEFLATE ...)

+2

: uncompress the file; ; . , , ( , ). .

; .

, - . SO, ; - . , , , .

+1

Source: https://habr.com/ru/post/1650715/


All Articles