Get Compression Method of Zip Entry Example

  1. /*
  2. Get Compression Method of Zip Entry Example
  3. This Java example shows how to get compression method used to
  4. compress the entry using getMethod method of Java ZipEntry class.
  5. */
  6.  
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.Enumeration;
  10. import java.util.zip.ZipEntry;
  11. import java.util.zip.ZipFile;
  12.  
  13.  
  14. public class GetCompressionMethod {
  15.  
  16. public static void main(String args[])
  17. {
  18. try
  19. {
  20. /*
  21. * To Open a zip file, use
  22. *
  23. * ZipFile(String fileName)
  24. * constructor of the ZipFile class.
  25. *
  26. * This constructor throws IOException for any I/O error.
  27. */
  28. ZipFile zipFile = new ZipFile("c:/FileIO/WebFiles.zip");
  29.  
  30. /*
  31. * Get list of zip entries using entries method of
  32. * ZipFile class.
  33. */
  34.  
  35. Enumeration e = zipFile.entries();
  36.  
  37. System.out.println("File Name\t\t\t\tCompression Method");
  38. System.out.println("---------------------------------");
  39.  
  40. while(e.hasMoreElements())
  41. {
  42. ZipEntry entry = (ZipEntry)e.nextElement();
  43.  
  44. /*
  45. * To get compression method used to compress entry, use
  46. *
  47. * int getMethod()
  48. * method of ZipEntry class.
  49. *
  50. * This method returns the method of compression, or -1
  51. * if not specified.
  52. *
  53. * Compression method can be either STORED or DEFLATED for
  54. * any zip entry.
  55. */
  56.  
  57. int method = entry.getMethod();
  58.  
  59. if(method == ZipEntry.DEFLATED)
  60. System.out.println(entry.getName() + "\t\t\t\tDeflated");
  61. else if(method == ZipEntry.STORED)
  62. System.out.println(entry.getName() + "\t\t\t\tStored");
  63. else if(method == -1)
  64. System.out.println(entry.getName() + "\t\t\t\tNot Specified");
  65.  
  66. }
  67.  
  68. /*
  69. * close the opened zip file using,
  70. * void close()
  71. * method.
  72. */
  73.  
  74. zipFile.close();
  75.  
  76. }
  77. catch(IOException ioe)
  78. {
  79. System.out.println("Error opening zip file" + ioe);
  80. }
  81. }
  82.  
  83. }
  84.  
  85. /*
  86. Output of this program would be
  87. File Name Compression Method
  88. ---------------------------------
  89. css/datagrid.css Deflated
  90. css/graph.css Deflated
  91. jsps/Masthead.jspf Deflated
  92. jsps/Welcome.jsp Deflated
  93. */

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!