1

I use swingworkers to extract zipfile and also append extraction prosecc to the textArea in the GUI. It extract only one item from zipped file and didn't show up anything in the textArea.

Can anyone suggest any solution?

public class UnzipWorkers extends SwingWorker<String,Void> {
private WebTextArea statusTextArea;
private File archive,outputDir;

public UnzipWorkers(WebTextArea statusTextArea,File archive,File outputDir) {
    this.archive=archive;
    this.outputDir=outputDir;
    this.statusTextArea = statusTextArea;
}

@Override
protected String doInBackground() throws Exception {
        statusTextArea.append(String.valueOf(System.currentTimeMillis()));
        try {
            ZipFile zipfile = new ZipFile(archive);
            for (Enumeration e = zipfile.entries(); e.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                unzipEntry(zipfile, entry, outputDir);

            }
        } catch (Exception e) {
            OeExceptionDialog.show(e);
        }

    return "Extracted successfully: " + archive.getName() + "\n";  
}

@Override
protected void done() {
    super.done();
    try {
        statusTextArea.append( get());
        FileTreePanel.btnRefresh.doClick();
    } catch (InterruptedException e) {
        e.printStackTrace();  
    } catch (ExecutionException e) {
        e.printStackTrace(); 
    }
}

private String unzipEntry(ZipFile zipfile, final ZipEntry entry, File outputDir)  {
    String success = "Extracted failed: "+ entry + "\n";
    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()){
        createDir(outputFile.getParentFile());
    }
    try {
        BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
        IOUtils.copy(inputStream, outputStream);
        outputStream.close();
        inputStream.close();
        success="Extracted successfully: " + entry + "\n";
    }catch (IOException io){
        OeExceptionDialog.show(io);
    }catch (NullPointerException n){
        OeExceptionDialog.show(n);
    }catch (ArithmeticException a){
        OeExceptionDialog.show(a);
    }
    return success;
}

private void createDir(File dir) {
    if (!dir.exists()) {
        try {
            dir.mkdirs();
        } catch (RuntimeException re) {
            OeExceptionDialog.show(re);
        }
    }
}
}
itro
  • 7,006
  • 27
  • 78
  • 121
  • 1
    I don't see where you are appending things to the textarea besides the first line of doInBackground and in done(). Are there more than one entry in your zipfile? Have you tried printin the stacktrace? Calling Swing code from SwingWorker is really not recommended. – Guillaume Polet May 07 '12 at 11:11
  • Don't show dialogs during a looping process, and especially don't show dialogs from the doInBackground() method (or from methods it calls). Instead SwingWorker.publish() your error messages, and then use SwingWorker.process() to update the gui. I'm really just saying what mKorbel said. – Jim May 08 '12 at 16:51

1 Answers1

5

SwingWorker isn't designated to works this way, for periodical output from doInBackground() are there methods process() and publish(), you can

1) use Runnable#Thread instead of SwingWorker then you code works by adding JTextArea.append(), wrapped into invokeLater()

2) add process() or publish() to the SwingWorker, inside this method put IOStream and append to the JTextArea periodically

3) I'd be suggest to use method get() for implemented reason

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I did change my code as you can see in above but still no effect. should i do something like updateUI after append to textArea? – itro May 07 '12 at 12:59
  • [code example, JProgressBar is JTextArea.append and noises around is your FileIO](http://stackoverflow.com/a/6114890/714968) – mKorbel May 07 '12 at 13:07
  • @itro: I don't see your implementation of `process()`, as mKorbel suggests. See also this [example](http://stackoverflow.com/a/4637725/230513). – trashgod May 07 '12 at 16:02