1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
/* Find File in a Zip File Example. This Java example shows how to find a particular file in a zip file using ZipFile and ZipEntry Java classes. */ import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class FindFileInZipFile { public static void main(String args[]) { try { //open the source zip file ZipFile sourceZipFile = new ZipFile("c:/SearchDemo.zip"); //File we want to search for inside the zip file String searchFileName = "readme.txt"; //get all entries Enumeration e = sourceZipFile.entries(); boolean found = false; System.out.println("Trying to search " + searchFileName + " in " + sourceZipFile.getName()); while(e.hasMoreElements()) { ZipEntry entry = (ZipEntry)e.nextElement(); /* * Here, normal compare would not work. * * Because zip might contain directories so the entry name will not * match extactly with the file name we want to search. * * Additionally, there might be more than one file with the same * name in different directories inside the zip archive. * * So approch here is to search using indexOf and not using * equals or equalsIgnoreCase methods. */ if(entry.getName().toLowerCase().indexOf(searchFileName) != -1) { found = true; System.out.println("Found " + entry.getName()); /* * if you want to search only first instance, uncomment the * following break statement. */ //break; } } if(found == false) { System.out.println("File :" + searchFileName + " Not Found Inside Zip File: " + sourceZipFile.getName()); } //close the zip file sourceZipFile.close(); } catch(IOException ioe) { System.out.println("Error opening zip file" + ioe); } } } /* Output of this program woule be Trying to search readme.txt in c:SearchDemo.zip Found xampplite/htdocs/drupal58/sites/all/README.txt Found xampplite/htdocs/fun610/modules/README.txt Found xampplite/htdocs/demo/sites/all/README.txt Found xampplite/htdocs/fun610/themes/README.txt Found xampplite/htdocs/knowledge/sites/all/README.txt */ |
Add Comment