Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6491622

ZipEntry.setSize() does not set the uncompressed size correctly.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.6.0-rc"
      Java(TM) SE Runtime Environment (build 1.6.0-rc-b104)
      Java HotSpot(TM) 64-Bit Server VM (build 1.6.0-rc-b104, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      SunOS 5.9 Generic_117171-11 sun4u sparc SUNW,Ultra-Enterprise
      Linux 2.4.26-openmosix1mt2 #1 Mon Apr 18 15:32:52 MDT 2005 i686 i686 i386 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      When trying to set the uncompressed size it does not save correctly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just replace the file to zip up with your own file and redirect the output to your own directory. When I take the test zip file and run zcat on it this is what I get for the header information.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Using file: /u/ramihu/test
      Open /u/ramihu/sumTxt and zip it up...
      Created the ZipEntry...
      ZIP ENTRY SIZE: 2322595
      File /u/ramihu/sumTxt zipped up and ready for read...
      Attempting to get the uncompressed zip size: 2322595
      Done using ZipInputStream for Information about the Zip entry. Trying ZipFile instead...
      Using /u/ramihu/test for test.
      Using a ZipEntry initialized through ZipFile.getName: /u/ramihu/sumTxt
      Entry Size is: 2322595
      ACTUAL -
      Using file: /u/ramihu/test
      Open /u/ramihu/sumTxt and zip it up...
      Created the ZipEntry...
      ZIP ENTRY SIZE: 2322595
      File /u/ramihu/sumTxt zipped up and ready for read...
      Attempting to get the uncompressed zip size: -1
      Done using ZipInputStream for Information about the Zip entry. Trying ZipFile instead...
      Using /u/ramihu/test for test.
      Using a ZipEntry initialized through ZipFile.getName: /u/ramihu/sumTxt
      Entry Size is: 2322595

      ******************* FROM THE COMMAND LINE ************************
      telinux9{ramihu}38> file test
      test: Zip archive data, at least v2.0 to extract
      telinux9{ramihu}39> zcat -l test
               compressed uncompressed ratio uncompressed_name
                   387872 -1 0.0% test
      telinux9{ramihu}40>

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package javatests;

      import java.io.BufferedOutputStream;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.util.Enumeration;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipFile;
      import java.util.zip.ZipInputStream;
      import java.util.zip.ZipOutputStream;

      public class Main {
          
          /** Creates a new instance of Main */
          public Main() {
          }
          
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              // TODO code application logic here
              byte[] buf = new byte[1024*8];
              
              try {
                  File zipOut = new File( "/u/ramihu/test" );
      // File zipOut = new File( "/u/summary/DEVL/sums/BDB/8Q/4B/11UNH" );
                  System.out.println( "Using file: " + zipOut.toString() );
                  File sourceFile = new File( "/u/ramihu/sumTxt" );
                  BufferedOutputStream fileBuf = new BufferedOutputStream( new FileOutputStream( zipOut ) );
                  
                  System.out.println("Open " + sourceFile.toString() + " and zip it up...");
                  
                  // Create the ZIP file
                  ZipOutputStream out = new ZipOutputStream( fileBuf );
                  
                  // Compress the files
                  FileInputStream in = new FileInputStream( sourceFile );
                  
                  ZipEntry ze = new ZipEntry( sourceFile.toString() );
                  ze.setSize( sourceFile.length() );
                  ze.setTime( sourceFile.lastModified() );
                  System.out.println( "Created the ZipEntry..." );
                  System.out.println("ZIP ENTRY SIZE: " + ze.getSize() );
                  // Add ZIP entry to output stream.
                  out.putNextEntry(ze);
                  
                  // Transfer bytes from the file to the ZIP file
                  int len;
                  while ((len = in.read(buf)) > 0) {
                      out.write(buf, 0, len);
                  }
                  
                  // Complete the entry
                  
                  out.closeEntry();
                  in.close();
                  
                  out.finish();
                  out.flush();
                  
                  // Complete the ZIP file
                  out.close();
                  
                  System.out.println( "File " + sourceFile.toString() + " zipped up and ready for read..." );
                  
                  // Now open it back up and check the data.
                  ZipInputStream in1 = new ZipInputStream(new FileInputStream(zipOut));
                  
                  // Get the first entry
                  ZipEntry entry = in1.getNextEntry();
                  System.out.println("Attempting to get the uncompressed zip size: " + entry.getSize());
                  
                  // Close the streams
                  in1.close();
                  System.out.println( "Done using ZipInputStream for Information about the Zip entry. Trying ZipFile instead..." );
                  
                  try {
                      // Open the ZIP file
                      ZipFile zf = new ZipFile(zipOut);
                      
                      System.out.println( "Using " + zipOut.toString() + " for test.");
                      
                      // Enumerate each entry
                      for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
                          // Get the entry name
                          ZipEntry ent = ((ZipEntry)entries.nextElement());
                          System.out.println( "Using a ZipEntry initialized through ZipFile.getName: " + ent.getName() );
                          System.out.println( "Entry Size is: " + ent.getSize() );
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  
              } catch (IOException e) {
                  // Need to throw some kind of an error here...
                  e.printStackTrace();
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Yes, make a system call to your platform zip and do it that way.

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: