2011-03-08 243 views
3

我有下面的代码,它检查是否存在于SD卡上,如果文件夹存在,我想添加另一个if语句来检查实际文件夹中是否有zip文件实际上存在。我能做些什么来检查文件夹中的zip扩展名。该文件夹应该有很多的拉链,但我只希望它检查以确保有拉链和没有其他文件扩展名。我感谢您对此提供任何帮助。Java压缩文件的检查目录

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(); 
} 

编辑: 谢谢你们在这里帮助是我结束了在你的帮助使用,我没有测试它尚未但它看起来不错。

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's a zip file in there.. 
      } else { 
      //no zips inside folder 
     } 
    } 

回答

6
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's a zip file in there.. 
} 

试试上面..

4

你看过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 

}