/*
 * @(#) CompressUtil.java 1.0 98/20/5
 * Copyright (c) 1998 Larry Rodrigues
 */

package compress;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;

public class CompressUtil {
   public static void gzip(String file){
      try{
         FileInputStream fis = new FileInputStream(new File(file));
         FileOutputStream fos = new FileOutputStream(new File(file+".gz"));
         GZIPOutputStream gos = new GZIPOutputStream(fos);
         byte[] fileBuf = new byte[512];
         for(;;){
             int bytesread = fis.read(fileBuf);
             if (bytesread <0) {break;}
             gos.write(fileBuf,0,bytesread);
         }
         gos.flush();
         gos.close();
      } catch(Exception e) {System.out.println(e);}
   }

   public static void gunzip(String file){
      if(!file.endsWith(".gz")) {
          //System.out.println("Not a gzip file");
          return;
      }
      try{
         FileInputStream fis = new FileInputStream(new File(file));
         GZIPInputStream gzis = new GZIPInputStream(fis);
         String outfile = file.substring(0,file.indexOf(".gz"));
         FileOutputStream fos = new FileOutputStream(new File(outfile));
         byte[] fileBuf = new byte[512];
         for(;;){
             int bytesread = gzis.read(fileBuf);
             if (bytesread <0) {break;}
             fos.write(fileBuf,0,bytesread);
         }
         fos.flush();
         fos.close();
      } catch(Exception e) {
           System.out.println(e);
      }
   }

   public static void zip(String outfile, String filelist[]){
      if(filelist == null) return;
      try{
         File of = outfile.endsWith(".zip")? new File(outfile): new File(outfile+".zip");
         FileOutputStream fos = new FileOutputStream(of);
         ZipOutputStream zos = new ZipOutputStream(fos);
         for(int i=0;i<filelist.length;i++){
             try{
                File file = new File(filelist[i]);
                FileInputStream fis = new FileInputStream(new File(filelist[i]));
                ZipEntry entry = new ZipEntry(filelist[i]);
                entry.setMethod(ZipEntry.DEFLATED);
                zos.putNextEntry(entry);
                byte[] fileBuf = new byte[512];
                for(;;){
                    int bytesread = fis.read(fileBuf);
                    if (bytesread <0) {break;}
                    zos.write(fileBuf,0,bytesread);
                }
                zos.closeEntry();
             } catch (Exception e){
                 e.printStackTrace();
             }
          }
          zos.flush();                                                                     zos.flush();
          zos.close();
      } catch(Exception e) {e.printStackTrace();}
   }

   public static void zip(String infile){
      try{
         File file = new File(infile);
         FileInputStream fis = new FileInputStream(new File(infile));
         FileOutputStream fos = new FileOutputStream(new File(infile+".zip"));
         ZipOutputStream zos = new ZipOutputStream(fos);
         ZipEntry entry = new ZipEntry(infile);
         zos.putNextEntry(entry);
         zos.setMethod(ZipEntry.DEFLATED);
         byte[] fileBuf = new byte[512];
         for(;;){
             int bytesread = fis.read(fileBuf);
             if (bytesread <0) {break;}
             zos.write(fileBuf,0,bytesread);
         }
         zos.closeEntry();
         zos.flush();
         zos.close();
      } catch(Exception e) {e.printStackTrace();}
   }

  /*** Unzips zip and jar files.
     * @param zipfile the complete path of the zip/jar file including its name.
     */
   public static void unzip(String zipfile){
      if(!(zipfile.endsWith(".zip") || zipfile.endsWith(".jar")) ) {
          System.out.println("Not a zip or jar file");
          return;
      }
      try {
        FileInputStream fis = new FileInputStream(zipfile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ZipInputStream zstream = new ZipInputStream(bis);
        ZipEntry zentry = null;
        while((zentry = zstream.getNextEntry())!=null) {
              String entryname = zentry.getName();
              File dir =null;
              //Check whether the entry is a directory.
              if(zentry.isDirectory()){
                 dir = new File(entryname);
                 if(!dir.exists()){
                    dir.mkdir();
                 }
                 continue;  //Skip the rest.
              }
              //Tokenize the entry name into path and file name.
              StringTokenizer tk = new StringTokenizer(entryname, "/");
              String stringdir = tk.nextToken();

              while(tk.hasMoreTokens()){
                    dir = new File(stringdir);
                    if(!dir.exists()){
                       dir.mkdir();
                    }
                    stringdir += File.separator+tk.nextToken();
              }

              //Create a file from the last token.
              File outfile = new File(stringdir);
              FileOutputStream fos = new FileOutputStream(outfile);
              byte[] buf = new byte[1024];
              for(;;){
                  int bytesread = zstream.read(buf);
                  if (bytesread <0) {break;}
                  fos.write(buf,0,bytesread);
              }
              fos.close();
        }// end of while
        zstream.close();
      }catch(Exception e){System.out.println("exception"+e);}
   }
}
