First of all, many thanks to the many hackers out there that sent me code
snippits to get my mind into the java.util.zip API.

Second, though none of the code I received was on target to what I was
trying to do, it gave me much insight into the Java I/O filtering streams.

Third, I am posting my class here and anybody can use it as they will.
 This - as it is - will take a command-line argument for a drive, zip all
the filtered files (see the code), and delete the unzipped files.

Fourth, you can modify this code easily for servlets to substitute the
command-line args[] input to a servlet.request parameter to accomplish the
same thing (which is my goal) so remote clients can zip directory files on
the server.  I am going to expand the input to allow clients to specify the
file extensions to be processed through the servlet.request mechanism.

Fifth, I received numerous public and private emails saying that I have been
harsh in responding to questions posted on these lists.  I apologize for
that; it was never my intention.  Email many times does not convey the true
attitude of the sender, and I have always tried to inject a sense of humor
in my responses (except, maybe for the people who request help who have
obviously not even read the documentation).  I do not mean to come across as
flippant or egoistic; I, like many of the guys (even Tania) here, only wish
to help those who are stuck on a particular problem (like I have with this
for the past week).  I suppose my trivial attempts at humor were translated
as sarcasm.  With the above-noted exception, this has never been my intent.

Finally,  This code is raw, and I have not refactored it, so if you have any
ideas to make it better, please post them.  What this exercise has taught
me, if nothing else, is that no class has ever been written to do precisely
what this class does.  Also, it proves that these lists work.  You guys are
awesome!

Cheers!
Mark

I am a deeply superficial person.  -Andy Warhol

This email was scanned with Norton AntiVirus 2002 before sending.

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

public class ZipUtil {
    private static File zipFile;

    public static void main( String[] args) {
        String path = args[ 0];
        if( path != null) {
            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( "\nUsing 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( "^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^");
                            } else {
                                // only add data files to .zip files
                                File f = new File( dir.getAbsolutePath(),
fileNames[ j]);

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

                System.out.println( "\nFile compression and cleanup
finished!");

            } catch( ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println( "A drive must be entered: <drive>:\\");
        }
    }

    protected static void addFilesToZip( Vector files) {
        if( files.size() != 0) {
            try {
                String zip = zipFile.getAbsolutePath().toString();
                File[] file = new File[ files.size()];
                System.out.println( "Number of files to compress to " + zip
+ ": " + files.size());
                Enumeration enum = files.elements();
                FileOutputStream fos = new FileOutputStream( zip);
                ZipOutputStream zos = new ZipOutputStream( new
BufferedOutputStream( fos));

                for( int i = 0; i < file.length; i++) {
                    file[ i] = (File) enum.nextElement();
                    BufferedReader br = new BufferedReader( new
FileReader( file[ i]));
                    zos.putNextEntry( new ZipEntry( file[ i].toString()));

                    System.out.println( "\nCompressing and adding file " +
file[ i] + " to " + zip + "....");
                    System.out.println( "Buffer size = " + br.read() + "
bytes");

                    int c = 0;
                    if( file[ i] != null) {
                        while(( c = br.read()) != -1)
                            zos.write( c);
                        br.close();
                    } else {
                        System.out.println( "File " + file[ i] + " is
null.");
                    }
                    System.out.println( "Deleting file: " + file[ i]);
                    file[ i].delete();
                }
                zos.close();
            } catch( NoSuchElementException e) {
                e.printStackTrace();
            } catch( FileNotFoundException e) {
                e.printStackTrace();
            } catch( ZipException e) {
                e.printStackTrace();
            } catch( IOException e) {
                e.printStackTrace();
            }
        }
    }
}

___________________________________________________________________________
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