How to I find out the size of a GZIP section embedded in firmware?

You can count the number of byte read using a custom InputStream. You would need to force the stream to read one byte at a time to ensure you don't read more than you need.


You can wrap your current InputStream in this

class CountingInputStream extends InputStream {
    final InputStream is;
    int counter = 0;

    public CountingInputStream(InputStream is) {
        this.is = is;
    }

    public int read() throws IOException {
        int read = is.read();
        if (read >= 0) counter++;
        return read;
    }
}

and then wrap it in a GZIPInputStream. The field counter will hold the number of bytes read.

To use this with BufferedInputStream you can do

 InputStream is = new BufferedInputStream(new FileInputStream(filename));
 // read some data or skip to where you want to start.

 CountingInputStream cis = new CountingInputStream(is);
 GZIPInputStream gzis = new GZIPInputStream(cis);
 // read some compressed data
 cis.read(...);

 int dataRead = cis.counter;