:::::::::::::: GZipAllFiles.java :::::::::::::: package harold; import java.io.*; import java.util.*; public class GZipAllFiles { public final static int THREAD_COUNT = 4; private static int filesToBeCompressed = -1; public static void main(String[] args) { Vector pool = new Vector(); GZipThread[] threads = new GZipThread[THREAD_COUNT]; for (int i = 0; i < threads.length; i++) { threads[i] = new GZipThread(pool); threads[i].start(); } int totalFiles = 0; for (int i = 0; i < args.length; i++) { File f = new File(args[i]); if (f.exists()) { if (f.isDirectory()) { File[] files = f.listFiles(); for (int j = 0; j < files.length; j++) { if (!files[j].isDirectory()) { // don't recurse directories totalFiles++; synchronized (pool) { pool.add(files[j]); pool.notifyAll(); } }// nota directory }// end for }// f a directory else { // f exists and is not a directory totalFiles++; synchronized (pool) { pool.add(0, f); pool.notifyAll(); } } } // end if f.exists(), do not take action if it does not exist. } // end for filesToBeCompressed = totalFiles; // make sure that any waiting thread knows that no // more files will be added to the pool for (int i = 0; i < threads.length; i++) { threads[i].interrupt(); } } public static int getNumberOfFilesToBeCompressed() { return filesToBeCompressed; } } :::::::::::::: GZipThread.java :::::::::::::: package harold; import java.io.*; import java.util.*; import java.util.zip.*; public class GZipThread extends Thread { private List pool; private static int filesCompressed = 0; public GZipThread(List pool) { this.pool = pool; } private static synchronized void incrementFilesCompressed() { filesCompressed++; } public void run() { File input = null; while (filesCompressed != GZipAllFiles.getNumberOfFilesToBeCompressed()) { synchronized (pool) { while (pool.isEmpty()) { if (filesCompressed == GZipAllFiles.getNumberOfFilesToBeCompressed()) return; try { pool.wait(); } catch (InterruptedException e) { } } input = (File) pool.remove(pool.size()-1); }// end synchronized block on pool. compress(input); } // end while not all files compressed. } // end run private void compress ( File input) { // don't compress an already compressed file if (!input.getName().endsWith(".gz")) { try { InputStream in = new FileInputStream(input); in = new BufferedInputStream(in); // output file with same name as input file and new extension. File output = new File(input.getParent(), input.getName() + ".gz"); if (!output.exists()) { // Don't overwrite an existing file OutputStream out = new FileOutputStream(output); out = new GZIPOutputStream(out); out = new BufferedOutputStream(out); int b; while ((b = in.read()) != -1) out.write(b); out.flush(); out.close(); incrementFilesCompressed(); in.close(); } } catch (IOException e) { System.err.println(e); } } // end if not compressed }//end compress } // end ZipThread