'How to randomly access a file in a zip file in an efficient way?

I want to parse a large file in a zip file. Parsing the file requires random access to it. In a word, the procedure is to read a header first, get some offsets from the header, and then seek to the offsets to read the real data. The actual structure is much more complex, so it's almost impossible to avoid random access.

The file isn't too large to fit in a byte[], but since it is still quite large (around 50M), and I may have to deal with many files like this at a time, I don't want to simply read all the bytes of it into a byte[]. I don't want to extract the whole file, then open it with RandomAccessFile either, due to performance overhead.

Here's basically what I want:

public class Foo {
    public static void main(String[] args) throws IOException {
        // In a zip file, unable to use RandomAccessFile
        var url = new URL("jar:file:/C:/path/to/jar/foo.zip!/foo/bar/large.dat");
        try (var stream = ?) {  // How to do this?
            // I can do something like...
            stream.seek(42);
            stream.read();
            stream.tell();
        }
    }
}

Can anyone tell me a way to get random access the file in an efficient way, please? Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source