package com.FXzip;

import com.FXzip.util.Util;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.*;
import javafx.fxml.*;
import javafx.scene.control.*;
import javax.swing.JFileChooser;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import org.apache.commons.io.FilenameUtils;

public class CompressorController implements Initializable {

    @FXML
    private ProgressBar pbar;

    @FXML
    private TextField txtInput;

    @FXML
    private TextField txtOutput;

    @FXML
    private TextField txtPass;

    @FXML
    private Button btnStart;

    @FXML
    private ComboBox<String> cbAction;

    private File input;

    private File outputDirectory;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

    @FXML
    public void browseInput(ActionEvent event) {
        input = showFileSelectionDialog(txtInput, JFileChooser.FILES_AND_DIRECTORIES,
                "Choose a file/diretory to compress or a file to decompress");
    }

    @FXML
    public void browseOutput(ActionEvent event) {
        outputDirectory = showFileSelectionDialog(txtOutput, JFileChooser.DIRECTORIES_ONLY,
                "Choose a diretory");
    }

    private File showFileSelectionDialog(TextField txtPath, int selectionMode, String dialogTitile) {
        File file = null;
        JFileChooser chooser = new JFileChooser(".");
        chooser.setDialogTitle(dialogTitile);
        chooser.setFileSelectionMode(selectionMode);
        int ret = chooser.showOpenDialog(null);
        if (ret == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            txtPath.setText(file.getPath());
        }

        return file;
    }

    @FXML
    public void start() throws IOException {
        if (input == null || outputDirectory == null) {
            Util.showErrorDialog("Error", "File Error", "You need to insert a valid path in input and output fields.");
            return;
        }

        String action = cbAction.getSelectionModel().getSelectedItem();

        if (action.equalsIgnoreCase("Decompress") && !FilenameUtils.getExtension(input.getName()).equals("zip")) {
            Util.showErrorDialog("Error", "File Error", "You need to insert a zip file in input.");
            return;
        }

        if (action.equals("Decompress")) {
            decompress();
        } else {
            compress();
        }
    }

    private void compress() {

    }

    private void decompress() throws IOException {
        try {

            ZipFile zipFile = new ZipFile(input.getPath());
            
            if (zipFile.isEncrypted()) {
                if (txtPass.getText().equals("")) {
                    zipFile.setPassword("1");
                } else {
                    zipFile.setPassword(txtPass.getText());
                }

            }


            List fileHeaderList = zipFile.getFileHeaders();

            for (int i = 0; i < fileHeaderList.size(); i++) {
                FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
                zipFile.extractFile(fileHeader, outputDirectory.getPath());
            }
                        
        } catch (ZipException ex) {
            Util.showErrorDialog("Error", "File Error", ex.getMessage());
        }
    }
}

@@ -121,32 +121,8 @@
 L;%0D%0A
-import java.util.List;%0D%0A
 impo
@@ -383,23 +383,31 @@
 p4j.
-model.FileHeade
+progress.ProgressMonito
 r;%0D%0A
@@ -928,26 +928,24 @@
 ndle rb) %7B%0D%0A
-%0D%0A
     %7D%0D%0A%0D%0A   
@@ -2491,32 +2491,64 @@
 ;%0D%0A        %7D%0D%0A%0D%0A
+        pbar.setProgress(0);%0D%0A%0D%0A
         if (acti
@@ -2841,32 +2841,63 @@
 );%0D%0A            
+zipFile.setRunInThread(true);%0D%0A
 %0D%0A            if
@@ -3149,18 +3149,16 @@
    %7D%0D%0A%0D%0A
-%0D%0A
         
@@ -3165,276 +3165,456 @@
     
-List fileHeaderList = zipFile.getFileHeaders();%0D%0A%0D%0A            for (int i = 0; i %3C fileHeaderList.size(); i++) %7B%0D%0A                FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);%0D%0A                zipFile.extractFile(fileHeader, outputDirectory.getPath());%0D%0A
+zipFile.extractAll(outputDirectory.getPath());%0D%0A            ProgressMonitor progressMonitor = zipFile.getProgressMonitor();%0D%0A%0D%0A            new Thread() %7B%0D%0A                @Override%0D%0A                public void run() %7B%0D%0A                    while (progressMonitor.getState() == ProgressMonitor.STATE_BUSY) %7B%0D%0A                        double d = ((double) progressMonitor.getPercentDone()) / 100;%0D%0A                        pbar.setProgress(d);%0D%0A        
     
@@ -3630,32 +3630,37 @@
 %0D%0A              
+  %7D%0D%0A
           %0D%0A    
@@ -3653,16 +3653,30 @@
         
+  %7D.start();%0D%0A
 %0D%0A      

decompress with progress bar.