001package ca.bc.webarts.tools;
002
003import java.io.BufferedInputStream;
004import java.io.BufferedOutputStream;
005import java.io.File;
006import java.io.FileNotFoundException;
007import java.io.FileOutputStream;
008import java.io.IOException;
009import java.io.InputStream;
010import java.util.Enumeration;
011import java.util.zip.ZipEntry;
012import java.util.zip.ZipException;
013
014import java.util.zip.ZipFile;
015
016/**
017 *  Description of the Class
018 *
019 * @author     unknowntgutwin
020 * @created    May 19, 2002
021 */
022public class UnZipper
023{
024
025  /**  The ZipFile that will get unzipped. * */
026  public ZipFile zf;
027
028  /**  Class Holder for an EOF flag. * */
029  public final static int EOF = -1;
030
031
032  /**
033   *  Constructor for the UnZipper object.
034   *
035   * @param  src  Description of the Parameter
036   * @param  des  Description of the Parameter
037   */
038  UnZipper(String src, String des)
039  {
040    unzipData(src, des);
041  }
042
043
044  /**
045   *  Constructor for the UnZipper object that unzips into the current dir.
046   *
047   * @param  src  Description of the Parameter
048   */
049  UnZipper(String src)
050  {
051    unzipData(src, System.getProperty("user.dir"));
052  }
053
054
055  /**
056   *  Main entry for this calss as an application.
057   *
058   *<code> Usage: java UnZipper ZIPFILENAME [Destination Dir]</code>
059   *
060   * @param  argv  Description of the Parameter
061   */
062  public static void main(String argv[])
063  {
064    if (argv.length == 2)
065    {
066      new UnZipper(argv[0], argv[1]);
067    }
068    else if (argv.length == 1)
069    {
070      new UnZipper(argv[0]);
071    }
072    else
073    {
074      System.out.println("Usage:java UnZipper zipfile /dir");
075    }
076  }
077
078
079  /**
080   *  Description of the Method
081   *
082   * @param  src      Description of the Parameter
083   * @param  destDir  Description of the Parameter
084   */
085  public void unzipData(String src, String destDir)
086  {
087    Enumeration myEnum;
088    try
089    {
090      zf = new ZipFile(src);
091      myEnum = zf.entries();
092
093      while (myEnum.hasMoreElements())
094      {
095
096        ZipEntry target = (ZipEntry) myEnum.nextElement();
097        System.out.print(target.getName() + " .");
098        saveEntry(destDir, target);
099        System.out.println(". unpacked");
100      }
101    }
102    catch (FileNotFoundException e)
103    {
104      System.out.println("zipfile not found");
105    }
106    catch (ZipException e)
107    {
108      System.out.println("zip error...");
109    }
110    catch (IOException e)
111    {
112      System.out.println("IO error...");
113    }
114  }
115
116
117  /**
118   *  Extracts (unzips) the passed in ZipEntry and saves it to a file.
119   *
120   * @param  s                 Description of the Parameter
121   * @param  target            Description of the Parameter
122   * @exception  ZipException  Description of the Exception
123   * @exception  IOException   Description of the Exception
124   */
125  public void saveEntry(String s, ZipEntry target)
126    throws ZipException, IOException
127  {
128    try
129    {
130      File file = new File(s + "/" + target.getName());
131      if (target.isDirectory())
132      {
133        file.mkdirs();
134      }
135      else
136      {
137        InputStream is = zf.getInputStream(target);
138        BufferedInputStream bis = new BufferedInputStream(is);
139        File dir = new File(file.getParent());
140        dir.mkdirs();
141        FileOutputStream fos = new FileOutputStream(file);
142        BufferedOutputStream bos = new BufferedOutputStream(fos);
143
144        int c;
145        while ((c = bis.read()) != EOF)
146        {
147          bos.write((byte) c);
148        }
149        bos.close();
150        fos.close();
151      }
152    }
153    catch (ZipException e)
154    {
155      throw e;
156    }
157    catch (IOException e)
158    {
159      throw e;
160    }
161  }
162
163}
164