如何在JCIFS中设置DosFileFilter,并将多个扩展名作为通配符?

时间:2013-02-01 23:06:40

标签: java jcifs

为了使用网络共享文件夹中的某些文件,我正在转移到JCIFS。到目前为止,我已经完成了以下测试,列出了我需要的文件(取自this example

public class ListDirectoryContent extends DosFileFilter {

    int count = 0;

    public ListDirectoryContent() {
        super("*.txt", 0xFFFF);
    }
    public ListDirectoryContent(String wildcard, int attributes) {
        super(wildcard, attributes);
    }

    public boolean accept(SmbFile file) throws SmbException {
        System.out.print( " " + file.getName() );
        count++;

        return (file.getAttributes() & attributes) != 0;
    }

    public static void main( String[] argv ) throws Exception {

        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("DOMAIN","myuser","A good password, yes sir!");
        SmbFile file = new SmbFile( "smb://networkResource/sharedFolder/Files/SubFolderOne/", auth);
        ListDirectoryContent llf = new ListDirectoryContent();

        long t1 = System.currentTimeMillis();
        SmbFile[] pepe = file.listFiles(llf);
        long t2 = System.currentTimeMillis() - t1;

        System.out.println();
        System.out.println( llf.count + " files in " + t2 + "ms" );

        System.out.println();
        System.out.println( pepe.length + " SmbFiles in " + t2 + "ms" );
    }
}

到目前为止,它适用于一个扩展名通配符。如何扩展dosfilefilter以检查一组扩展? (像commons.io.FileUtils那样)

1 个答案:

答案 0 :(得分:2)

我写了这个基本的SmbFilenameFilter来在文件名中使用通配符。希望这有帮助!

private static class WildcardFilenameFilter implements SmbFilenameFilter {
    private static final String DEFAULT_WIDLCARD = "*";

    private final String wildcard = DEFAULT_WIDLCARD;
    private final String regex;

    public WildcardFilenameFilter(String filename) {
        regex = createRegexPattern(filename);
    }

    @Override
    public boolean accept(SmbFile dir, String name) throws SmbException {
        return name.matches(regex);
    }

    private String createRegexPattern(String filename) {
        return filename.replace(".", "\\.").replace(wildcard, ".+");
    }
}