Find File in a Zip File Example

  1. /*
  2. Find File in a Zip File Example.
  3. This Java example shows how to find a particular file
  4. in a zip file using ZipFile and ZipEntry Java classes.
  5. */
  6.  
  7. import java.io.IOException;
  8. import java.util.Enumeration;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipFile;
  11.  
  12. public class FindFileInZipFile {
  13.  
  14. public static void main(String args[])
  15. {
  16. try
  17. {
  18. //open the source zip file
  19. ZipFile sourceZipFile = new ZipFile("c:/SearchDemo.zip");
  20.  
  21. //File we want to search for inside the zip file
  22. 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 not
  38. * 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 using
  44. * 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 the
  53. * 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 file
  66. sourceZipFile.close();
  67.  
  68. }
  69. catch(IOException ioe)
  70. {
  71. System.out.println("Error opening zip file" + ioe);
  72. }
  73. }
  74.  
  75.  
  76. }
  77.  
  78. /*
  79. Output of this program woule be
  80.  
  81. Trying to search readme.txt in c:\SearchDemo.zip
  82. Found xampplite/htdocs/drupal58/sites/all/README.txt
  83. Found xampplite/htdocs/fun610/modules/README.txt
  84. Found xampplite/htdocs/demo/sites/all/README.txt
  85. Found xampplite/htdocs/fun610/themes/README.txt
  86. Found xampplite/htdocs/knowledge/sites/all/README.txt
  87. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!