Java Inflator class can be used to deal with both cases, but first we must check for the header. The first two bytes denote the header and it is a simple check :
static boolean isZlibHeader(byte[] bytes) {
//deal with java stupidity : convert to signed int before comparison
char byte1 = (char)(bytes[0] & 0xFF);
char byte2 = (char)(bytes[1] & 0xFF);
return byte1 == 0x78 && (byte2 == 0x01 || byte2 == 0x9c || byte2 == 0xDA);
}
private void inflateToFile(byte[] encBytes, int offset, int size, BufferedOutputStream f) throws IOException {
Inflater inflator = new Inflater(true);
inflator.setInput(encBytes, isZlibHeader(encBytes) ? offset+2 : offset, isZlibHeader(encBytes) ? size-2 : size);
byte[] buf = new byte[4096];
int nbytes = 0;
do {
try {
nbytes = inflator.inflate(buf);
if (nbytes > 0) {
f.write(buf,0,nbytes);
}
} catch (DataFormatException e) {
//handle error
}
} while (nbytes > 0);
inflator.end();
}
An example URL that had to be processed this way : http://game4fun.square7.ch
Here is the Wireshark capture, showing the Content-Encoding set to deflate as well as the de-chunked header (the first 2 bytes "78 9c") at the lower bottom pane of the display: