Get Compressed Size of Zip Entry Example

  1. /*
  2. Get Compressed Size of Zip Entry Example
  3. This Java example shows how to get compressed size of particular entry
  4. (i.e. file or directory) using getCompressedSize method of
  5. Java ZipEntry class.
  6. */
  7.  
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.Enumeration;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipFile;
  13.  
  14.  
  15. public class GetCompressedSize {
  16.  
  17. public static void main(String args[])
  18. {
  19. try
  20. {
  21. /*
  22. * To Open a zip file, use
  23. *
  24. * ZipFile(String fileName)
  25. * constructor of the ZipFile class.
  26. *
  27. * This constructor throws IOException for any I/O error.
  28. */
  29. ZipFile zipFile = new ZipFile("c:/FileIO/WebFiles.zip");
  30.  
  31. /*
  32. * Get list of zip entries using entries method of
  33. * ZipFile class.
  34. */
  35.  
  36. Enumeration e = zipFile.entries();
  37.  
  38. System.out.print("File Name");
  39. System.out.print("\t\t\t\tCompressed Size");
  40. System.out.println("---------------------------------");
  41.  
  42. while(e.hasMoreElements())
  43. {
  44. ZipEntry entry = (ZipEntry)e.nextElement();
  45.  
  46. /*
  47. * To get compressed size of zip entry, use
  48. *
  49. * long getCompressedSize()
  50. * method of ZipEntry class.
  51. *
  52. * This method returns the size of compressed entry or -1
  53. * if the size is not known.
  54. *
  55. * PLEASE NOTE that uncompressed size and compressed size
  56. * of the zip entries created using ZipEntry.STORED method
  57. * will be same.
  58. *
  59. * However, It differes for zip entries created using
  60. * ZipEntry.DEFLATED entries.
  61. */
  62.  
  63. String entryName = entry.getName();
  64. long compressedSize = entry.getCompressedSize();
  65.  
  66. System.out.print(entryName);
  67. System.out.print("\t\t\t\t" + compressedSize);
  68. System.out.print("\n");
  69.  
  70. }
  71.  
  72. /*
  73. * close the opened zip file using,
  74. * void close()
  75. * method.
  76. */
  77.  
  78. zipFile.close();
  79.  
  80. }
  81. catch(IOException ioe)
  82. {
  83. System.out.println("Error opening zip file" + ioe);
  84. }
  85. }
  86.  
  87. }
  88.  
  89. /*
  90. Output of this program would be
  91. File Name Compressed Size
  92. ---------------------------------
  93. css/datagrid.css 3255
  94. css/graph.css 416
  95. jsps/Masthead.jspf 536
  96. jsps/Welcome.jsp 1248
  97. */

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!