I am new to Commons VFS and am trying to understand how to open a nested archive. I found an example in the source code that deals with this. This test passes.
@Test
void testTGZ() throws Exception {
FileSystemManager fsManager = VFS.getManager();
String uri = "tgz:file:///c:/data/nested.tgz!/test.tgz";
FileObject archive = fsManager.resolveFile(uri);
FileObject nestedFs = fsManager.createFileSystem(archive);
// List the children of the archive file
FileObject[] children = nestedFs.getChildren();
for (FileObject child : children) {
System.out.println(child.getURI());
}
}
whereas this one fails with
org.apache.commons.vfs2.FileSystemException: Could not find a file provider that can handle file "tgz:file:///C:/data/nested.tar.gz!/test.tar.gz".
@Test
void testTarGZ() throws Exception {
FileSystemManager fsManager = VFS.getManager();
String uri = "tgz:file:///c:/data/nested.tar.gz!/test.tar.gz";
FileObject archive = fsManager.resolveFile(uri);
FileObject nestedFs = fsManager.createFileSystem(archive);
// List the children of the archive file
FileObject[] children = nestedFs.getChildren();
for (FileObject child : children) {
System.out.println(child.getURI());
}
}
Other than the names the files are the same so why would one work and not the other? What am I doing wrong?
I came across the same issue and after debugging I was able to find the source of the problem.
If you take a look at
createFileSystem(FileObject file)in theDefaultFileSystemManagerclass, you can find that it uses the methodgetScheme(FileObject file)from theFileTypeMapclass to detect the scheme:When the extension is
tgz,mimeTypeis evaluated tonulland the method tries to get the scheme using the extension itself. In this case,tgzis returned fromextensionMapandDefaultFileSystemManageruses that value to select aFileProvider(TarFileProviderin this case).When the extension is
tar.gz,mimeTypeis parsed asapplication/octet-streamwhich doesn't have any mapping inmimeTypeMapand the methods returnsnull. In this case,DefaultFileSystemManagerfails to create a file system because noFileProviderwas found.Solution:
You can specify the
FileProviderwhile creating the file system: