00001 package org.hfbk.util;
00002
00003 import java.awt.BorderLayout;
00004 import java.awt.Frame;
00005 import java.awt.Label;
00006 import java.io.File;
00007 import java.io.FileOutputStream;
00008 import java.io.IOException;
00009 import java.io.InputStream;
00010 import java.net.URL;
00011 import java.util.Enumeration;
00012 import java.util.zip.ZipEntry;
00013 import java.util.zip.ZipFile;
00014 import java.util.zip.ZipInputStream;
00015
00016 import javax.swing.JFileChooser;
00017 import javax.swing.JOptionPane;
00018
00019
00020 public class Installer {
00021
00022 public static void unzip(String zipfile, String targetdir) throws IOException{
00023
00024 ZipFile jar = new ZipFile(zipfile);
00025
00026 Enumeration<? extends ZipEntry> fs=jar.entries();
00027
00028 while ( fs.hasMoreElements()) {
00029 ZipEntry file = fs.nextElement();
00030
00031 if (file.isDirectory()) continue;
00032
00033 File f = new File(targetdir + File.separator + file.getName());
00034
00035 if (f.exists() && f.lastModified()>file.getTime()) continue;
00036 System.out.println("Extracting "+f.getPath());
00037 String path=f.getParent();
00038 new File(path).mkdirs();
00039
00040 InputStream is = jar.getInputStream(file);
00041 FileOutputStream fos = new FileOutputStream(f);
00042 byte[] buffer = new byte[4096];
00043 int read;
00044 while ((read=is.read(buffer))>0)
00045 fos.write(buffer, 0, read);
00046 fos.close();
00047 is.close();
00048 }
00049 }
00050
00051 public static void unzip(URL zipfile, String targetdir) throws IOException{
00052 ZipInputStream jar=new ZipInputStream(zipfile.openStream());
00053
00054 ZipEntry file;
00055 while ((file=jar.getNextEntry())!=null) {
00056
00057 if (file.isDirectory()) continue;
00058
00059 File f = new File(targetdir + File.separator + file.getName());
00060
00061 if (f.exists() && f.lastModified()>file.getTime()) continue;
00062 System.out.println("Extracting "+f.getPath());
00063 String path=f.getParent();
00064 new File(path).mkdirs();
00065
00066 FileOutputStream fos = new FileOutputStream(f);
00067 byte[] buffer = new byte[4096];
00068 int read;
00069 while ((read=jar.read(buffer))>0)
00070 fos.write(buffer, 0, read);
00071 fos.close();
00072 }
00073 jar.close();
00074 }
00075
00076
00077 static void install(File dir){
00078 try {
00079 URL source=Installer.class.getProtectionDomain().getCodeSource().getLocation();
00080 System.out.println(source);
00081
00082 unzip(source, dir.getAbsolutePath()+File.separator+"VisClient");
00083 } catch (IOException e) {
00084
00085 throw new RuntimeException (e);
00086 }
00087 }
00088
00089 public Installer() {
00090 Frame f=new Frame("Vis/Installer");
00091 f.setUndecorated(true);
00092 f.add(new Label("Select directory where the VisClient directory should be created:", Label.CENTER),BorderLayout.NORTH);
00093 JFileChooser fc=new JFileChooser(""){
00094 public void approveSelection() {
00095 setVisible(false);
00096 install(getSelectedFile());
00097 JOptionPane.showMessageDialog(this, "Yo.");
00098 System.exit(0);
00099 }
00100 public void cancelSelection() {
00101 System.exit(1);
00102 }
00103 };
00104 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
00105 f.add(fc);
00106
00107 UIUtils.blackify(f);
00108 f.setSize(640, 480); f.setVisible(true);
00109 }
00110
00111 public static void main(String[] args) throws IOException {
00112 new Installer();
00113 }
00114 }