Search code examples
swtprogress-bareclipse-rcpunzipwizard

Need progress bar at the time of unzip in SWT wizard by java


I have implement SWT wizard appliation, In this application i performed unzip file programatically and i need a progress bar during unzip. Please find the below code for unzip,

 public static void extract(File zipfile, File outdir)
      {
        try
        {
          ZipInputStream zin = new ZipInputStream(new FileInputStream (zipfile));
          ZipEntry entry;
          String name, dir;
          while ((entry = zin.getNextEntry()) != null)
          {
            name = entry.getName();
            if( entry.isDirectory() )
            {
              mkdirs(outdir,name);
              continue;
            }
            /* this part is necessary because file entry can come before
             * directory entry where is file located
             * i.e.:
             *   /foo/foo.txt
             *   /foo/
             */
            dir = dirpart(name);
            if( dir != null )
              mkdirs(outdir,dir);

            extractFile(zin, outdir, name);

          }

          zin.close();
        } 
        catch (IOException e)
        {
          e.printStackTrace();
        }
      } 

At the wizard page i call this method like this,

    btnUnzip.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {

                    File file = new File(text.getText());
                    File file1 = new File(textToSaveUnzipFile.getText());
                    UnzipUtility.extract(file, file1);
      }
   }

Solution

  • If you are using a JFace Wizard you can use the built in progress monitor in the wizard.

    In the Constructor of your Wizard class call:

    setNeedsProgressMonitor(true);
    

    To show progress call

    getContainer().run(true, true, runnable);
    

    This call can be in the Wizard or a WizardPage.

    where runnable is a class implementing IRunnableWithProgress. The run method of this class will look something like:

    @Override
    public void run(final IProgressMonitor monitor)
      throws InterruptedException
    {
      monitor.beginTask("Title", .. number of work steps ..);
    
      try
       {
         while (not finished) {
            ... do a small amount of work
    
            // Update progress
            monitor.worked(1);
         }
       }
      finally
       {
         monitor.done();
       }
    }