import java.lang.*;
import java.util.*;
import java.io.*;
import java.util.zip.*;

import javax.swing.*;                                           
import java.awt.*;
import java.awt.event.*;                                                  
import java.awt.Toolkit.*;
import java.awt.datatransfer.*;





class unzipwindowc extends JFrame implements WindowListener, ActionListener{
  JList listbox;
  JScrollPane listboxscroller;
  DefaultListModel list;
  JLabel statusbar;
  unzipwindowc() {
    list = new DefaultListModel();
    listbox = new JList(list);
    listboxscroller = new JScrollPane(listbox);
    JMenuBar mb = new JMenuBar();
          
    JMenu m = new JMenu("file");
    mb.add(m);
          
    JMenuItem miopen = new JMenuItem("open zip file");
    m.add(miopen);
    miopen.setActionCommand("menuopen");    
    miopen.addActionListener(this);
          
    JMenuItem minew = new JMenuItem("extract");
    m.add(minew);
    minew.setActionCommand("menuextract");    
    minew.addActionListener(this);

    JMenuItem misave = new JMenuItem("extract all");
    m.add(misave);
    misave.setActionCommand("menuextractall");    
    misave.addActionListener(this);
          
    JMenuItem miexit = new JMenuItem("exit");
    m.add(miexit);
    miexit.setActionCommand("menuexit");    
    miexit.addActionListener(this);
          
    SpringLayout spl = new SpringLayout();
    this.addWindowListener(this);
    this.setJMenuBar(mb);        
    this.setBounds(0,0,600,600);
    this.getContentPane().setLayout(spl);
    spl.putConstraint(SpringLayout.EAST,listboxscroller,0,SpringLayout.EAST,this.getContentPane());
    spl.putConstraint(SpringLayout.WEST,listboxscroller,0,SpringLayout.WEST,this.getContentPane());

    statusbar = new JLabel("status bar");
    spl.putConstraint(SpringLayout.SOUTH,statusbar,0,SpringLayout.SOUTH,this.getContentPane());
    spl.putConstraint(SpringLayout.SOUTH,listboxscroller,0,SpringLayout.NORTH,statusbar);
    spl.putConstraint(SpringLayout.NORTH,listboxscroller,0,SpringLayout.NORTH,this.getContentPane());
    this.getContentPane().add(statusbar);
    this.getContentPane().add(listboxscroller);
  }
  
  
  public void windowActivated(WindowEvent e) {
    System.out.println("Activated event");
    unzipapp.unzipwindow.requestFocusInWindow();
  }
  public  void windowClosed(WindowEvent e) {
    System.out.println("Close event");
  }
  public void windowClosing(WindowEvent e) {
    System.out.println("Closing event");
    try {
      System.exit(0);
    } catch(SecurityException err) {
      System.out.println("can not exit");
    }
  }
  public void windowDeactivated(WindowEvent e) {
    System.out.println("Deactivated event");
  }
  public void windowDeiconified(WindowEvent e) {
    System.out.println("Deiconified event");
  }
  public void windowIconified(WindowEvent e) {
    System.out.println("Iconified event");
  }
  public void windowOpened(WindowEvent e) {
    System.out.println("Opened event");
  }

  public void actionPerformed(ActionEvent e) {
    String action = e.getActionCommand();
    System.out.println("action " + action);
    if (action.equals("menunew")) {
      System.out.println("new");
    }
    if (action.equals("menuopen")) {
      System.out.println("open");
      FileDialog fd = new FileDialog(unzipapp.unzipwindow,"open zip file",FileDialog.LOAD);
      fd.show();
      String filename = fd.getDirectory() + fd.getFile();
      if (fd.getFile() != null) {
        try {
          unzipapp.zipfile = new ZipFile(filename);
          unzipapp.zipdirectory = fd.getDirectory();
          unzipapp.unzipwindow.openzipfile();
        } catch (Exception err) {
          System.out.println(err.getMessage());
        }
      }
      unzipapp.unzipwindow.requestFocusInWindow();
    }
    if (action.equals("menuextract")) {
      int i = unzipapp.unzipwindow.listbox.getSelectedIndex();
      boolean extract_file = true;
      if (i > -1){
        String filename = (String) unzipapp.unzipwindow.list.get(i);
        String str = JOptionPane.showInputDialog("what directory should the files be extract to?",unzipapp.zipdirectory);
        if (str != null) {
          String str2 = unzipapp.zipdirectory;
          if (str.length() == 0){
            str = str + File.separator;
          }
          if (str.charAt(str.length()-1) != File.separatorChar) {
            str = str + File.separator;
          }
          if (str.length() == 1){
            int option = JOptionPane.showConfirmDialog(unzipapp.unzipwindow,new JLabel("extract to the root directory?"),"question",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
            if (option == JOptionPane.NO_OPTION) {         
              extract_file = false;
            }
          }
          if (extract_file == true) {
            unzipapp.zipdirectory = str;
            unzipapp.unzipwindow.extractfile(filename);
            unzipapp.zipdirectory = str2;
          }
        }
      } else {
        JOptionPane.showMessageDialog(unzipapp.unzipwindow,new JLabel("no file selected"),"error",JOptionPane.ERROR_MESSAGE);
      }
    }
    if (action.equals("menuextractall")) {
      String str = JOptionPane.showInputDialog("what directory should the files be extract to?",unzipapp.zipdirectory);
      if (str != null) {
        String str2 = unzipapp.zipdirectory;
        if (str.length() == 0){
          str = str + File.separator;
        }
        if (str.charAt(str.length()-1) != File.separatorChar) {
          str = str + File.separator;
        }
        unzipapp.zipdirectory = str;
        unzipapp.unzipwindow.extractallfiles();
        unzipapp.zipdirectory = str2;
      }
    }
    if (action.equals("menuexit")) {
      System.out.println("exit");
      try {
        System.exit(0);
      } catch(SecurityException err) {
        System.out.println("can not exit");
      }
    }
  }
  void openzipfile() {
    list.clear();
    Enumeration e = unzipapp.zipfile.entries();
    while(e.hasMoreElements()) {
      ZipEntry entry = (ZipEntry) e.nextElement();
      list.addElement(entry.getName());
    }
  }
  int extractfile(String filename) {
    byte[] buffer = new byte[4096];
    int l = 0;
    String path = null;
    try {
      File f = new File(unzipapp.zipdirectory+filename);
      ZipEntry e = unzipapp.zipfile.getEntry(filename);
      File d = new File(f.getParent());
      String path2 = d.getPath();
      File f2 = new File(filename);
      path = f2.getParent();
      if (d.exists() == false){
        int option = JOptionPane.showConfirmDialog(unzipapp.unzipwindow,new JLabel("create the directory " + path + "?"),"question",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
        if (option == JOptionPane.YES_OPTION) {         
          d.mkdirs();
        } else {
            return 0;
        }
      }
      if (e.isDirectory() == false) {
        if (f.exists() == false) {
          InputStream in = unzipapp.zipfile.getInputStream(e);
          BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(unzipapp.zipdirectory+filename,true));
          l = in.read(buffer);
          while (l > 0){
            out.write(buffer,0,l);
            l = in.read(buffer);
          }
          in.close();
          out.close();
        } else {
          JOptionPane.showMessageDialog(unzipapp.unzipwindow,new JLabel(filename + " already exists"),"error",JOptionPane.ERROR_MESSAGE);
        }
      }
    } catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(unzipapp.unzipwindow,new JLabel("the path " + path + " does not exist "),"error",JOptionPane.ERROR_MESSAGE);
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return 1;
  }
  void extractallfiles() {
    if (unzipapp.zipfile != null) {
      Enumeration e = unzipapp.zipfile.entries();
      while(e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        int a = extractfile(entry.getName());
        if (a == 0){break;}
      }
    } else {
      JOptionPane.showMessageDialog(unzipapp.unzipwindow,new JLabel("no zip file selected"),"error",JOptionPane.ERROR_MESSAGE);    
    }
  }


}

class unzipapp {

  static unzipwindowc unzipwindow;
  static JLabel statusbar;
  static ZipFile zipfile = null;
  static String zipdirectory = null;
  static String programname = "frank's simple file extractor ";
  static double programversion = 1.0;

  public static void main(String[] args ){
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          JMenuBar mb = new JMenuBar();
          
          unzipwindow = new unzipwindowc();
          unzipwindow.setTitle(programname + "  v " + programversion);
          unzipwindow.setVisible(true);
        }
    });
  }
}

