
  package com.vistech.util;
  import java.awt.*;
  import java.io.*;
  import java.util.*;
  import java.util.zip.*;
  import java.util.jar.*;
  import java.net.*;
  public class JarUtil {
     public final static int BY_DATE =1;
     public final static int BY_ALPHA =2;
     /**
       * Filters the file names in a jar and lists them.
       * @param jarpath the complete path name of the jar file including the file name.
       * @param filter an array of file name extensions.
       * @return an array of file names.
       */
     public static String[] listJarFiles(String jarpath, String[] filter){
        try{
           File jarfile = new File(jarpath);
           return listJarEntries(jarfile, filter);
        }catch(Exception e){
           e.printStackTrace();
        }
        return null;
     }

     public static String[] listJarFiles(File file,
                                         String[] filter,
                                         int sortMode){
        if(file ==null) return null;
        String[] list = null;
        try{
           JarFile jarfile = new JarFile(file);
           list = listJarEntries(jarfile, filter, sortMode);
        }catch(Exception e){
           //e.printStackTrace();
        }
        return list;
     }

     /**
       * Lists the files in a jar.
       * @param jarfile the jar file.
       * @param filter an array of file name extensions.
       * @return an array of file names.
       */
     public static String[] listJarEntries(JarFile jarfile, String[] filter){
        String fileList[] = null;
        
        try{
            Vector list = new Vector();
            for(Enumeration e = jarfile.entries(); e.hasMoreElements();){
                String name = ((JarEntry)e.nextElement()).getName();
                if(filterFileNames(name,filter))
                   list.addElement(name);
            }
            fileList = new String[list.size()];
            list.copyInto(fileList);
        } catch (Exception e){
           System.out.println("Jar file reading exception "+ e);}
           return fileList;
     }

     public static String[] listJarEntries(JarFile jarfile,
                                             String[] filter,
                                             int sortMode){
        final int sort = sortMode;
        String fileList[] = null;
        try{
            String name = null;
            long date;
            Comparator cmp = new Comparator(){
                public int compare(Object aa, Object bb){
                   FileAttributes faa = (FileAttributes) aa;
                   FileAttributes fab = (FileAttributes) bb;
                   if((aa==null)|| (bb==null)) return -1;
                   switch(sort) {
                     case BY_DATE :
                       if(faa.date > fab.date) return 1;
                       else if(faa.date == fab.date){
                               if(faa.index > fab.index) return 1;
                               else return -1;
                             } else  return -1;

                     case BY_ALPHA :
                       return faa.name.compareTo(fab.name);
                     default:
                       return -1;
                   }
                }
            };

            TreeSet tset = new TreeSet(cmp);
            int ind =0;
            for(Enumeration e = jarfile.entries(); e.hasMoreElements();){
                JarEntry entry = (JarEntry)e.nextElement();
                name = entry.getName();
                if(filterFileNames(name,filter)) {
                   date = entry.getTime();
                   FileAttributes attr = new FileAttributes(ind++,date,name);
                   tset.add(attr);
                }
            }
            jarfile.close();

            if(tset == null) return null;
            Iterator it = tset.iterator();
            fileList= new String[tset.size()];
            int i=0;
            while(it.hasNext()){
                 FileAttributes en = (FileAttributes)(it.next());
                 fileList[i++] = (String)(en.name);
            }
        } catch (Exception e){ System.out.println( e);}
            return fileList;
     }


     static class FileAttributes {
        public int index=0;
        public long date=0;
        public String name = null;
        public FileAttributes(int ind, long dat, String nam){
           index =ind;
           date =dat;
           name = nam;
        }
     }


     static class DateKey {
        public int index=0;
        public long date=0;
        public DateKey(int ind, long dat){
           index =ind;
           date =dat;
        }
     }
     

     /**
       * Lists the files in a zip file.
       * @param jarfile the jar file.
       * @param filter an array of file name extensions.
       * @return an array of file names.
       */
     public static String[] listZipEntries(ZipFile zipfile, String[] filter){
        String fileList[] = null;
        try{
            Vector list = new Vector();
            for(Enumeration e = zipfile.entries(); e.hasMoreElements();){
                String name = ((ZipEntry)e.nextElement()).getName();
                if(filterFileNames(name,filter))
                   list.addElement(name);
            }
            fileList = new String[list.size()];
            list.copyInto(fileList);
        } catch (Exception e){
           System.out.println("Zip file reading exception "+ e);}
           return fileList;
     }


     /**
       * Filters the file names in a jar and lists them.
       * @param jarfile the jar file.
       * @param filter an array of file name extensions.
       * @return an array of file names.
       */
     public static String[] listJarFiles(File jarfile, String[] filter){
        String fileList[] = null;
        try{
           ZipFile zipfile = new ZipFile(jarfile);
            Vector list = new Vector();
            for(Enumeration e = zipfile.entries(); e.hasMoreElements();){
                String name = ((ZipEntry)e.nextElement()).getName();
                if(filterFileNames(name,filter))
                   list.addElement(name);
            }
            fileList = new String[list.size()];
            list.copyInto(fileList);
        } catch (Exception e){ System.out.println("Zip file reading exception "+ e);}
           return fileList;
    }

     /**
       * Filters the file names in a jar and lists them.
       * @param jarfile the jar file.
       * @param filter an array of file name extensions.
       * @return an array of file names.
       */
     public static String[] listJarEntries(File jarfile, String[] filter){
        String fileList[] = null;
        try{
           ZipFile zipfile = new ZipFile(jarfile);
            Vector list = new Vector();
            for(Enumeration e = zipfile.entries(); e.hasMoreElements();){
                String name = ((ZipEntry)e.nextElement()).getName();
                if(filterFileNames(name,filter))
                   list.addElement(name);
            }
            fileList = new String[list.size()];
            list.copyInto(fileList);
        } catch (Exception e){ System.out.println("Zip file reading exception "+ e);}
           return fileList;
    }
     /**
       * Filters file names.
       * @param name the file name.
       * @param filter an array of file name extensions
       */
     public static boolean filterFileNames(String name, String filter[]){
        if(filter == null) return true;
        for(int i=0; i< filter.length;i++){
            if(name.endsWith(filter[i])) return true;
        }
        return false;
     }
     public static boolean isImage(String fl){
        String[] filter = {"jpeg", "jpg", "JPEG", "JPG", "gif", "GIF"};
        return filterFileNames(fl, filter);
     }
   
}

