Java validation directory for zip files

I have the code below that checks if a folder exists on the SD card, I would like to add another if statement if the folder exists to check if there are zip files in the folder if it really exists. What can I do to check the folder for the zip extension. There should be a lot of zip codes in the folder, but I want it to be checked to make sure there are zip files and there is no other file extension. I thank you for any help with this.

File z = new File("/mnt/sdcard/folder");
if(!z.exists()) {
Toast.makeText(MainMethod.this, "/sdcard/folder Not Found!", Toast.LENGTH_LONG).show(); 
} else {
Toast.makeText(MainMethod.this, "/sdcard/folder Found!", Toast.LENGTH_LONG).show();
}

EDIT: Thanks guys for the help, that’s what I ended up using with your help, I haven’t tested it yet, but I feel good.

    File z = new File("/mnt/sdcard/Folder");
    if(!z.exists()) {
           //create folder
} else {
        FilenameFilter f2 = new FilenameFilter() {
        public boolean accept(File dir, String filename) {
        return filename.endsWith("zip");
        }
        };
            if (z.list(f2).length > 0) {
            // there a zip file in there..
            } else {
            //no zips inside folder
        }
    }
+3
source share
2 answers
File f = new File("folder");
FilenameFilter f2 = new FilenameFilter() {
public boolean accept(File dir, String filename) {
return filename.endsWith("zip");
}
};
if (f.list(f2).length > 0) {
// there a zip file in there..
}

Try the above.

+6

FileNameFilter?

File f = new File("/mnt/sdcard/folder");
if(e.exist()){//file exist ??

File[] matchingFiles = f.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.endsWith("zip");
    }
});//list out files with zip at the end

}
+4

All Articles