Over 500 Magazines for FREE!

Yes! You can subscribe to ALL of them. They will be shipped to your home FREE of cost!
Kindly click here to apply!

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.




Do you have a better example?
We're sure you have hundreds of Java program examples.

Spare some time and submit your java example here even if you think it's too small to contribute.

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




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