How to set up a DosFileFilter in JCIFS with multiple extensions as wildcard?

StackOverflow https://stackoverflow.com/questions/14656594

  •  06-03-2022
  •  | 
  •  

سؤال

In order to use some files from a network shared folder I'm moving to JCIFS. So far I've done the following as a test to list the files I need (taken from 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" );
    }
}

And so far it works for one extension wildcard. How can I broaden the dosfilefilter to check for a set of extensions? (like commons.io.FileUtils does)

هل كانت مفيدة؟

المحلول

I wrote this basic SmbFilenameFilter to use wildcards in filenames. Hope this helps!

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, ".+");
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top