5 Replies Latest reply: Feb 5, 2013 11:48 AM by rp0428 RSS

    ZipInputStream & ZipEntry taking time to unzip big file

    Ravi_Teja
      Hi,

      We have big zip file around 2GB which needs to downloaded and unzipped. We are using ZipInputStream & ZipEntry for unzipping the files.
      This is taking around 45 min to unzip the file. Do we have better alternative for doing the same?

      Thanks in advance

      Regards,
      Ravi
        • 1. Re: ZipInputStream & ZipEntry taking time to unzip big file
          EJP
          Better alternative than what? Post some code.
          • 2. Re: ZipInputStream & ZipEntry taking time to unzip big file
            Ravi_Teja
            Below is the code we are using:
            public void getZipFiles(String sSourcePath, String sDestPath)
                {
                    try
                    {
                        sDestPath = sDestPath + Util.getFileSeparator();
                        byte[] buf = new byte[1024];
                        ZipInputStream zipinputstream = null;
                        ZipEntry zipentry;
                        BufferedOutputStream  outBuffer =null;
                        BufferedInputStream inBuffer = null;
                        zipinputstream = new ZipInputStream(
                            new FileInputStream(sSourcePath));
                        String sFile =sSourcePath.substring(0,  sSourcePath.indexOf("."));
                        
                    File f = new File(sFile);
                    if(f.mkdir())
                        System.out.println( f + " Directory Created");
                    else
                    System.out.println(f + " Directory is not created");
            
                        zipentry = zipinputstream.getNextEntry();
                        while (zipentry != null)
                        {
                            //for each entry to be extracted
                            String entryName = zipentry.getName();
                            int n;
            
                            if(zipentry.isDirectory())
                            {
            
                                File f1 = new File(sDestPath+zipentry.getName());
                                 if(f1.mkdir())
                                        System.out.println(f + " Directory Created");
                                    else
                                    System.out.println(f + " Directory is not created");
                            }
                            else
                            {
                            FileOutputStream fileoutputstream;
                            File newFile = new File(entryName);
                            String directory = newFile.getParent();
                            if(directory == null)
                            {
                                if(newFile.isDirectory())
                                    break;
                            }
            
                            fileoutputstream = new FileOutputStream(sDestPath+entryName);
                            outBuffer = new BufferedOutputStream(fileoutputstream);
                            inBuffer = new BufferedInputStream(zipinputstream);
            
                           /* while ((n = zipinputstream.read(buf, 0, 1024)) > -1)
                                fileoutputstream.write(buf, 0, n);*/
                            while(true){
                               int bytedata = inBuffer.read();
                              if(bytedata == -1)       break;
                              outBuffer.write(bytedata);
                            }
                            outBuffer.flush();
                            fileoutputstream.close();
                            fileoutputstream.flush();
                            }
                           // zipinputstream.closeEntry();
                            zipentry = zipinputstream.getNextEntry();
            
                        }//while
            
                        zipinputstream.close();
                        outBuffer.close();
                        inBuffer.close();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            Edited by: EJP on 5/02/2013 22:07: code tags
            • 3. Re: ZipInputStream & ZipEntry taking time to unzip big file
              Kayaman
              Why do you have the block read parts commented out?
              Even if you're using BufferedStreams, I wouldn't do it one byte at a time, that's gotta worsen the performance a bit at least.
              • 4. Re: ZipInputStream & ZipEntry taking time to unzip big file
                EJP
                Is this your real code? You can't flush a stream after you close it, so I'm surprised it execute at all. There is no need to flush it before closing; and there is no need to flush FileOutputStreams at all. And closing a stream closes all the streams wrapped around it, so closing the BufferedOutputStream is futile.

                To improve the performance I would restore the block code, and raise the buffer size from 1k to at least 16k. I would also make sure there is a BufferedInputStream under the ZipInputStream.
                • 5. Re: ZipInputStream & ZipEntry taking time to unzip big file
                  rp0428
                  >
                  while(true){
                  int bytedata = inBuffer.read();
                  if(bytedata == -1) break;
                  outBuffer.write(bytedata);
                  }
                  >
                  That code works with one byte at a time.
                  >
                  /* while ((n = zipinputstream.read(buf, 0, 1024)) > -1)
                  fileoutputstream.write(buf, 0, n);*/
                  >
                  That code, which you are NOT using works with 1024 bytes at a time.

                  If your mom asks you to go to the store and get her three gallons of milk would you make three trips and get one gallon each time? Or would you make one trip and get all three gallons at once?

                  See this simple example; there are plenty of others on the net.
                  http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/