Thanks to the many who sent me code snippets to orient me around the
java.util.zip API.  Unfortunately, nothing that was sent, and nothing I can
find on the net does recursive searching for filtered files for a set
directory depth on a named logical drive.  So I wrote a small class that
(almost does this).  It's doing everything right except adding the data
files to the .zip file.  Anybody want to eyeball this and let me know if I'm
doing something obviously st00pid?  I can't turn this into a servlet until I
get the algorithm working....

Thanks,
Mark
-----------------------------------------------------
import java.io.*;
import java.util.*;
import java.util.zip.*;

public class ZipUtil {
    private static File zipFile = null;

    public static void main( String[] args) {
        String path = args[ 0];

        try {
            File dirPathName = new File( path);
            String[] dirNames = dirPathName.list();

            for( int i = 0; i < dirNames.length; i++) {
                Vector v = new Vector();
                // get the directories and ignore the Windoze stuff
                if( dirNames[ i].equalsIgnoreCase( "RECYCLED") ||
                    dirNames[ i].equalsIgnoreCase( "RECYCLED") ||
                    dirNames[ i].equalsIgnoreCase( "TEMP") ||
                    dirNames[ i].equalsIgnoreCase( "TMP") ||
                    dirNames[ i].equalsIgnoreCase( "System Volume
Information") ||
                    dirNames[ i].equalsIgnoreCase( "pagefile.sys"))
continue;

                File dir = new File( dirPathName.getPath(), dirNames[ i]);
                String[] fileNames = dir.list();

                for( int j = 0; j < fileNames.length; j++) {
                    // get the filtered files, create zipfile if necessary
                    if( fileNames[ j] != null &&
                        ( fileNames[ j].endsWith( ".ub") ||
                        fileNames[ j].endsWith( ".ubu") ||
                        fileNames[ j].endsWith( ".hh") ||
                        fileNames[ j].endsWith( ".zip"))) {

                        if( fileNames[ j].endsWith( ".zip")) {
                            // use the existing .zip file
                            zipFile = new File( dir.getPath(), fileNames[
j]);
                            System.out.println( "Using zipfile: " +
zipFile);
                        } else {
                            // there is no preexisting .zip file so create
one
                            if( zipFile == null) {
                                zipFile = new File( dir.getPath(),
"data.zip");
                            }
                        }

                        if( fileNames[ j].endsWith( ".zip")) {
                            // don't add .zip files to .zip files
                            System.out.println( "zipfile = " + fileNames[
j]);
                        } else {
                            // only add data files to .zip files
                            File f = new File( dir.getPath(), fileNames[
j]);

                            if( f.isFile() &&
f.getCanonicalFile().toString().indexOf( "\\") == 2) {
                                // only scan top-level directories and add
to the file list
                                v.add( f);
                            }
                        }
                    }
                }
                addFilesToZip( v);
            }
        } catch( IOException e) {
            e.printStackTrace();
        } catch( ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }

    public static void addFilesToZip( Vector files) {
        Enumeration enum = files.elements();

        try {
            while( enum.hasMoreElements()) {
                String file = enum.nextElement().toString();
                String zip = zipFile.getCanonicalPath()toString();
                FileOutputStream fos = new FileOutputStream( zip);
                ZipOutputStream zos = new ZipOutputStream( new
BufferedOutputStream( fos));
                System.out.println( "\nCompressing and adding file " + file
+ " to " + zip + "....");
                BufferedReader br = new BufferedReader( new FileReader(
file));
                zos.putNextEntry( new ZipEntry( file));

                int len = 0;
                while(( len = br.read()) != -1) {
                    zos.write( len);
                }

                br.close();
            }

        } catch( NoSuchElementException e) {
            e.printStackTrace();
        } catch( IOException e) {
            e.printStackTrace();
        }
    }
}



This email was scanned with Norton AntiVirus 2002 before sending.

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to