TermCommentPost For Commonly Used Java Classes

7 Comments

    • 12.public class FindFileInZipFile {13. 14.public static void main(String args[])15.{16. try17. {18.//open the source zip file19.ZipFile sourceZipFile = new ZipFile(“c:/SearchDemo.zip”);20. 21.//File we want to search for inside the zip file22.String searchFileName = “readme.txt”;23. 24.//get all entries 25.Enumeration e = sourceZipFile.entries();26.boolean found = false;27. 28.System.out.println(“Trying to search ” + searchFileName + ” in ” + sourceZipFile.getName());29. 30.while(e.hasMoreElements())31.{32.ZipEntry entry = (ZipEntry)e.nextElement();33. 34./*35. * Here, normal compare would not work.36. * 37. * Because zip might contain directories so the entry name will not38. * match extactly with the file name we want to search.39. * 40. * Additionally, there might be more than one file with the same 41. * name in different directories inside the zip archive.42. * 43. * So approch here is to search using indexOf and not using44. * equals or equalsIgnoreCase methods.45. */46.if(entry.getName().toLowerCase().indexOf(searchFileName) != -1)47.{48.found = true;49.System.out.println(“Found ” + entry.getName());50. 51./*52. * if you want to search only first instance, uncomment the53. * following break statement.54. */55. 56.//break;57.}58.}59. 60.if(found == false)61.{62.System.out.println(“File :” + searchFileName + ” Not Found Inside Zip File: ” + sourceZipFile.getName());63.}64. 65. //close the zip file66.sourceZipFile.close();67. 68. }69. catch(IOException ioe)70. {71.System.out.println(“Error opening zip file” + ioe);72. }73.}74. 75. 76.}

Sponsors

Facebook Fans

Online Shopping – Flipkart