import javax.swing.*;
import java.io.*;
import java.util.zip.*;

/**
 * A utility class for deleting java class files.  Use this class before zipping
 * up a project.
 */

public class ClassDeleter {
    private static boolean verbose = false;
    private static int compressionLevel = Deflater.DEFAULT_COMPRESSION;
    private ZipOutputStream zip = null;
    private int delCount = 0;
    private int zipCount = 0;

    public ClassDeleter() {
        File root;
        JFileChooser chooser = new JFileChooser();

        //get the directory from which the user wants to delete .class files
        chooser.setFileSelectionMode(chooser.DIRECTORIES_ONLY);
        chooser.setDialogTitle("Select a directory to process");
        if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
            try{
                createZip();
                //get the directory
                root = chooser.getSelectedFile();
                //Confirm the deletion
                int rtn = JOptionPane.showConfirmDialog(null,
                    "Are you sure you want to process files in \n" +
                    root.getAbsolutePath(), "Confirm",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE);
                if (rtn == JOptionPane.NO_OPTION ){
                    System.exit(0);
                }
                //Blast away!
                processFile( root );
                closeZip();

                //Print out a count of deleted files
                System.out.println();
                System.out.println(delCount + " files deleted");
                System.out.println(zipCount + " files zipped");
            }catch (Exception e) {
                e.printStackTrace();
            }

        }

        System.exit(1);
    }

    private void processFile( File root ) throws IOException{
        //get a list of all the files in this directory
        File[] files = root.listFiles();
        //iterate through the files
        for (int i=0; i<files.length;i++){
            //recursively call this method if the file is a directory
            if (files[i].isDirectory()){
                processFile( files[i] );
            } else {
                //if the file is a Java class, then delete it
                if( isDeletable( files[i] ) ){
                    if( files[i].delete() ){
                        System.out.println( "deleted " + files[i].getName() );
                        delCount++;
                    }
                } else {
                    //this is a file we want, so zip it
                    addToZip(files[i]);
                }
            }
        }
    }

    public static void main(String[] args) {
        ClassDeleter classDeleter1 = new ClassDeleter();
    }

    private boolean isDeletable( File file ){
        //get the file's extension
        int index = file.getName().lastIndexOf('.');
        if (index > 0 ){
            String extension = file.getName().substring(index+1);
            //if it's equal to "class" then mark it for deletion
            if (extension.equals( "class" )){
                return true;
            }
        }
        //Also blow away temp files (marked with a tilde)
        if (file.getName().substring(file.getName().length()-1).equals("~")){
            return true;
        }
        return false;
    }

    private void createZip() throws FileNotFoundException{
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Enter name of zip file");
        if (chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
            zip = new ZipOutputStream(
                    new FileOutputStream(chooser.getSelectedFile().getAbsolutePath()));
            zip.setComment("created by Darcy Schultz");
            zip.setMethod(ZipOutputStream.DEFLATED);
            zip.setLevel(compressionLevel);
        }
    }

    private void addToZip( File file) throws FileNotFoundException, IOException{
        String name;
        int index;
        //make sure the outputStream is there
        if (zip==null)
            return;

        zipCount++;
        System.out.println( "adding " + file.getName() );
        // read a file into memory
        FileInputStream in = new FileInputStream(file);
        byte[] bytes = new byte[in.available()];
        in.read(bytes);
        in.close();

        // create and initialize a zipentry for it
        name = file.getAbsolutePath();
        index = name.indexOf(':');
        if (index>=0){
            index+=2;
            name=name.substring(index);
        }
        ZipEntry entry = new ZipEntry(name);
        entry.setTime(file.lastModified());

        // write the entry header, and the data, to the zip
        zip.putNextEntry(entry);
        zip.write(bytes);

        // write the end-of-entry marker to the zip
        zip.closeEntry();
    }

    private void closeZip() throws IOException{
        if (zip != null)
            zip.close();
    }

}

1