在内部zipfile Commons VFS中查找文件

时间:2017-05-30 11:50:32

标签: java apache-commons-vfs

是否可以使用FileObject :: findFiles方法或类似方法搜索存储在文件夹中的ZIP文件?或者我必须自己打开zipfiles?

FileObject root = vfs.resolveFile(file:///home/me/test/vfsdir);
// shows everything except the content of the zip 
FileObject[] allFiles = root.findFiles(Selectors.SELECT_ALL);    
// should contain only the three xmls
FileObject[] xmlFiles = root.findFiles(xmlSelector);

VFS目录树

/ (root)
/folderwithzips
/folderwithzips/myzip.zip (Zipfile not a folder)
/folderwithzips/myzip.zip/myfile.xml
/folderwithzips/myzip.zip/myfile2.xml
/folderwithzips/other.zip 
/folderwithzips/other.zip/another.xml

1 个答案:

答案 0 :(得分:0)

可悲的是,它无法在VFS中搜索拉链内容,就好像它们是文件夹一样。

所以我必须手动加载每个zip并在内容上执行我的选择器。

这个小方法对我有用。

public static void main(String[] vargs) throws FileSystemException {
    FileSystemManager manager = VFS.getManager();
    FileObject root = manager.resolveFile("/home/me/test/vfsdir");

    List<FileObject> files = findFiles(root, new XMLSelector());
    files.stream().forEach(System.out::println);
}

public static List<FileObject> findFiles(FileObject root,FileSelector fileSelector) throws FileSystemException {
    List<FileObject> filesInDir = Arrays.asList(root.findFiles(fileSelector));
    FileObject[] zipFiles = root.findFiles(new ZipSelector());

    FileSystemManager manager = VFS.getManager();

    List<FileObject> filesInZips = new ArrayList<>();
    for (FileObject zip: zipFiles){
        FileObject zipRoot = manager.createFileSystem(zip);
        Stream.of(zipRoot.findFiles(fileSelector)).forEach(filesInZips::add);
    }

    return Stream.concat(filesInDir.stream(),filesInZips.stream()).collect(Collectors.toList());
}