FileDocCategorySizeDatePackage
GZipThread.javaAPI DocExample1723Sat Sep 09 19:52:38 BST 2000None

GZipThread.java

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() {
    
    while (filesCompressed != GZipAllFiles.getNumberOfFilesToBeCompressed()) {
    
      File input = null;
      
      synchronized (pool) {         
        while (pool.isEmpty()) {
          if (filesCompressed == GZipAllFiles.getNumberOfFilesToBeCompressed()) return;
          try {
            pool.wait();
          }
          catch (InterruptedException e) {
          }
        }

        input = (File) pool.remove(pool.size()-1); 
      
      }
    
      // don't compress an already compressed file
      if (!input.getName().endsWith(".gz")) { 
      
        try {
          InputStream in = new FileInputStream(input);
          in = new BufferedInputStream(in);
          
          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 
  
    } // end while
    
  } // end run

} // end ZipThread