Determine if the Zip Entry Is Directory Example

  1. /*
  2. Determine if the Zip Entry Is Directory Example
  3. This Java example shows how to determine if the entry is directory
  4. of a file using isDirectory method of Java ZipEntry class.
  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.  
  13. public class DetermineDirectory {
  14.  
  15. public static void main(String args[])
  16. {
  17. try
  18. {
  19. /*
  20. * To Open a zip file, use
  21. *
  22. * ZipFile(String fileName)
  23. * constructor of the ZipFile class.
  24. *
  25. * This constructor throws IOException for any I/O error.
  26. */
  27. ZipFile zipFile = new ZipFile("c:/FileIO/WebFiles.zip");
  28.  
  29. /*
  30. * Get list of zip entries using entries method of
  31. * ZipFile class.
  32. */
  33.  
  34. Enumeration e = zipFile.entries();
  35.  
  36. System.out.print("File Name");
  37. System.out.print("\t\t\t\tIs Directory");
  38. System.out.print("\n---------------------------------\n");
  39.  
  40. while(e.hasMoreElements())
  41. {
  42. ZipEntry entry = (ZipEntry)e.nextElement();
  43.  
  44. /*
  45. * To determine if the entry is a directory, use
  46. *
  47. * boolean isDirectory()
  48. * method of Java ZipEntry class.
  49. *
  50. * This method returns true is the entry is directory,
  51. * false otherwise.
  52. *
  53. * Typically, directory entry is the one which contains
  54. * "/" at the end.
  55. */
  56.  
  57. String entryName = entry.getName();
  58. boolean isDir = entry.isDirectory();
  59.  
  60. System.out.print(entryName);
  61. System.out.print("\t\t\t\t" + isDir);
  62. System.out.print("\n");
  63.  
  64. }
  65.  
  66. /*
  67. * close the opened zip file using,
  68. * void close()
  69. * method.
  70. */
  71.  
  72. zipFile.close();
  73.  
  74. }
  75. catch(IOException ioe)
  76. {
  77. System.out.println("Error opening zip file" + ioe);
  78. }
  79. }
  80.  
  81. }
  82.  
  83. /*
  84. Output of this program would be
  85. File Name Is Directory
  86. ---------------------------------
  87. css/datagrid.css false
  88. css/graph.css false
  89. jsps/Masthead.jspf false
  90. jsps/Welcome.jsp false
  91. */

Post new comment

To combat spam, please enter the code in the image.


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!