Accessing nested archives using vfs2 apache library

I've spent way too much time finding out how to do this, so I will put the solution here - for future me if not for anyone else.
The scenario is as follows: we have a tar file, inside of which, in some subdirectories, there is a tar.gz file. Accessing files from tar.gz using vfs gave me quite a headache, so here is the solution:

This is how you initialize virtual file system object, declared here as DefaultFileSystemManager vfs:


My main error here was that I've skipped line 15 - it was described as optional in some tutorials. It is not in this scenario, however.

Having defined the vfs, we can access the contents. I already know location of tar.gz inside tar, so I am providing its path as an argument (pkgPath) and its name as well (pkgName). If you don't know the location you can iterate through children listed using fileObject.getChildren().
 Code looks like that:

Here the most problem I had was in lines 12 and 16. We have to remember that tar.gz is a tar file that is gzipped, so when we access it using vfs we get tar file first, then we have to access this tar, only then we have the files inside available. 
To quickly go through it step by step:
line 12 - we prepare string for vhs.resolveFile() method. It needs to know how to access the comperssed file (using gz:, which in previous snippet we've associated with Gzip file provider); and the exclamation mark is mandatory at the end, so vfs knows we are looking inside compressed file.
line 13 - we've created fileObject - this is tar.gz but we can execute getChildren on it to see what is compressed inside; of course it is a single tar file
line 16 - we need the same procedure as in line 12, i. e. tell vfs how to access a file (tar:) and tell it that we will look inside compressed file (exclamation mark).
From then on, it is easy-peasy.

Komentarze