Skip to Content

Get Compressed Size of Zip Entry Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  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. */