Get Uncompressed Size of Zip Entry Example

  1. /*
  2. Get Uncompressed Size of Zip Entry Example
  3. This Java example shows how to get uncompressed size of particular entry
  4. (i.e. file or directory) using getSize 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. public class GetUncompressedSize {
  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.print("File Name");
  38. System.out.print("\t\tUncompressed Size");
  39. System.out.println("\t\tCompressed Size");
  40.  
  41. System.out.println("---------------------------------");
  42.  
  43. while(e.hasMoreElements())
  44. {
  45. ZipEntry entry = (ZipEntry)e.nextElement();
  46.  
  47. /*
  48. * To get uncompressed size of zip entry, use
  49. *
  50. * long getSize()
  51. * method of ZipEntry class.
  52. *
  53. * This method returns the size of uncompressed entry or -1
  54. * if the size is not known.
  55. *
  56. * PLEASE NOTE that uncompressed size and compressed size
  57. * of the zip entries created using ZipEntry.STORED method
  58. * will be same.
  59. *
  60. * However, It differes for zip entries created using
  61. * ZipEntry.DEFLATED entries.
  62. */
  63. String entryName = entry.getName();
  64. long originalSize = entry.getSize();
  65. long compressedSize = entry.getCompressedSize();
  66.  
  67. System.out.print(entryName);
  68. System.out.print("\t\t" + originalSize);
  69. System.out.print("\t\t" + compressedSize);
  70. System.out.print("\n");
  71.  
  72.  
  73.  
  74. }
  75.  
  76. /*
  77. * close the opened zip file using,
  78. * void close()
  79. * method.
  80. */
  81.  
  82. zipFile.close();
  83.  
  84. }
  85. catch(IOException ioe)
  86. {
  87. System.out.println("Error opening zip file" + ioe);
  88. }
  89. }
  90.  
  91. }
  92.  
  93. /*
  94. Output of this program would be
  95. File Name Uncompressed Size Compressed Size
  96. ---------------------------------
  97. css/datagrid.css 26939 3255
  98. css/graph.css 2412 416
  99. jsps/Masthead.jspf 1200 536
  100. jsps/Welcome.jsp 3062 1248
  101. */

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!