Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ZipMe.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): 
00023  */
00024 
00025 package org.objectweb.cjdbc.common.util;
00026 
00027 import java.io.BufferedInputStream;
00028 import java.io.BufferedOutputStream;
00029 import java.io.File;
00030 import java.io.FileInputStream;
00031 import java.io.FileOutputStream;
00032 import java.io.IOException;
00033 import java.util.zip.ZipEntry;
00034 import java.util.zip.ZipInputStream;
00035 import java.util.zip.ZipOutputStream;
00036 
00037 import org.objectweb.cjdbc.common.i18n.Translate;
00038 import org.objectweb.cjdbc.common.log.Trace;
00039 
00040 /**
00041  * Zip utility class inspired by the java tutorials of sun.
00042  * 
00043  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00044  */
00045 public class ZipMe
00046 {
00047   // Buffers and streams
00048   private FileOutputStream     fos                        = null;
00049   private ZipOutputStream      zos                        = null;
00050   private ZipInputStream       zis                        = null;
00051   private FileInputStream      fis                        = null;
00052   private BufferedOutputStream dest;
00053   private String               rootDir;
00054   private String               zipPath;
00055   private boolean              useCompression             = true;
00056 
00057   static final int             BUFFER                     = 2048;
00058   
00059 
00060   // Store policy
00061   static final int             STORE_FULL_PATH_IN_ZIP     = 0;
00062   static final int             STORE_NAME_ONLY_IN_ZIP     = 1;
00063   static final int             STORE_RELATIVE_PATH_IN_ZIP = 2;
00064   static final int             STORE_PATH_FROM_ZIP_ROOT   = 3;
00065   static final int             STORE_POLICY               = STORE_PATH_FROM_ZIP_ROOT;
00066 
00067   static Trace                 logger                     = Trace
00068                                                               .getLogger(ZipMe.class
00069                                                                   .getName());
00070 
00071   /**
00072    * New ZipMe object
00073    */
00074   public ZipMe()
00075   {
00076   }
00077 
00078   /**
00079    * Create a zip file from directory
00080    * 
00081    * @param zipname of the file to create
00082    * @param rootdir of the directory to archive
00083    * @throws Exception if fails
00084    */
00085   public void create(String zipname, String rootdir) throws Exception
00086   {
00087     if (zipname == null || rootdir == null)
00088       throw new Exception("Invalid arguments to create zip file");
00089     this.rootDir = rootdir;
00090     try
00091     {
00092       zipPath = zipname + Constants.ZIP_EXT;
00093       fos = new FileOutputStream(zipPath);
00094       zos = new ZipOutputStream(fos);
00095 
00096       dirFunc(rootdir);
00097 
00098       zos.flush();
00099       zos.finish();
00100       zos.close();
00101       fos.close();
00102     }
00103     catch (IOException e)
00104     {
00105       logger.error(e.getMessage());
00106       throw e;
00107     }
00108   }
00109 
00110   /**
00111    * Same as above but the place where to put the zip file is explicit
00112    * 
00113    * @param zipname of the file to create
00114    * @param rootdir of the directory to archive
00115    * @param putMeThere please
00116    * @throws Exception if fails
00117    */
00118   public void create(String zipname, String rootdir, String putMeThere)
00119       throws Exception
00120   {
00121     create(putMeThere + File.separator + zipname, rootdir);
00122   }
00123 
00124   /**
00125    * Same as below but the place where to get the zip file is explicit
00126    * 
00127    * @param zipname of the file to expand
00128    * @param targetdir where to place unzipped files
00129    * @param getMeThere please
00130    * @throws Exception if fails
00131    */
00132   public void expand(String zipname, String targetdir, String getMeThere)
00133       throws Exception
00134   {
00135     expand(getMeThere + File.separator + zipname, targetdir);
00136   }
00137 
00138   /**
00139    * Get the size of the zip file
00140    * 
00141    * @return size in bytes
00142    */
00143   public long getZipSize()
00144   {
00145     File f = new File(zipPath);
00146     if (f.exists())
00147       return f.length();
00148     else
00149       return -1;
00150   }
00151 
00152   /**
00153    * Expand the content of the zip file
00154    * 
00155    * @param zipname of the file to expand
00156    * @param targetdir where to place unzipped files
00157    * @throws Exception if fails
00158    */
00159   public void expand(String zipname, String targetdir) throws Exception
00160   {
00161     File ftargetDir = new File(targetdir);
00162     if (!ftargetDir.exists())
00163       ftargetDir.mkdirs();
00164     if (!ftargetDir.exists())
00165       throw new Exception(Translate.get("zip.invalid.target.directory"));
00166 
00167     zipPath = zipname + Constants.ZIP_EXT;
00168     File fzipname = new File(zipPath);
00169     if (!fzipname.exists())
00170       throw new Exception(Translate.get("zip.invalid.source.file", zipname));
00171 
00172     try
00173     {
00174       fis = new FileInputStream(fzipname);
00175       zis = new ZipInputStream(fis);
00176       
00177       ZipEntry entry;
00178 
00179       byte data[] = new byte[BUFFER];
00180       while ((entry = zis.getNextEntry()) != null)
00181       {
00182         int count;
00183         String target = targetdir + File.separator + entry.getName();
00184         File fget = new File(target);
00185         fget.mkdirs(); // create needed new directories
00186         fget.delete(); // delete directory but not parents
00187         if (logger.isDebugEnabled())
00188           logger.debug(Translate.get("zip.extracting", new String[]{
00189               String.valueOf(entry), fget.getAbsolutePath()}));
00190         fos = new FileOutputStream(target);
00191 
00192         dest = new BufferedOutputStream(fos, BUFFER);
00193         while ((count = zis.read(data, 0, BUFFER)) != -1)
00194         {
00195           dest.write(data, 0, count);
00196         }
00197         dest.flush();
00198         dest.close();
00199       }
00200       zis.close();
00201     }
00202     catch (Exception e)
00203     {
00204       e.printStackTrace();
00205       logger.error(e.getMessage());
00206       throw e;
00207     }
00208   }
00209 
00210   private void dirFunc(String dirName) throws IOException
00211   {
00212     File dirObj = new File(dirName);
00213     if (dirObj.exists() == true)
00214     {
00215       if (dirObj.isDirectory() == true)
00216       {
00217 
00218         File[] fileList = dirObj.listFiles();
00219 
00220         for (int i = 0; i < fileList.length; i++)
00221         {
00222           if (fileList[i].isDirectory())
00223           {
00224             dirFunc(fileList[i].getPath());
00225           }
00226           else if (fileList[i].isFile())
00227           {
00228 
00229             zipFunc(fileList[i].getPath());
00230           }
00231         }
00232       }
00233       else
00234       {
00235         if (logger.isDebugEnabled())
00236           logger.debug(Translate.get("zip.not.directory", dirName));
00237       }
00238     }
00239     else
00240     {
00241       if (logger.isDebugEnabled())
00242         logger.debug(Translate.get("zip.directory.not.found", dirName));
00243     }
00244   }
00245 
00246   private void zipFunc(String filePath) throws IOException
00247   {
00248     File ffilePath = new File(filePath);
00249     String path = "";
00250     switch (STORE_POLICY)
00251     {
00252       case STORE_FULL_PATH_IN_ZIP :
00253         path = ffilePath.getAbsolutePath();
00254         break;
00255       case STORE_NAME_ONLY_IN_ZIP :
00256         ffilePath.getName();
00257         break;
00258       case STORE_RELATIVE_PATH_IN_ZIP :
00259         File f = new File("");
00260         String pathToHere = f.getAbsolutePath();
00261         path = ffilePath.getAbsolutePath();
00262         path = path.substring(path.indexOf(pathToHere + File.separator)
00263             + pathToHere.length());
00264         break;
00265       case STORE_PATH_FROM_ZIP_ROOT :
00266         path = ffilePath.getAbsolutePath();
00267         String tmpDir = File.separator + rootDir + File.separator;
00268         path = rootDir + File.separator
00269             + path.substring(path.indexOf(tmpDir) + tmpDir.length());
00270         logger.debug(path);
00271         break;
00272       default :
00273         break;
00274     }
00275 
00276     FileInputStream fis = new FileInputStream(filePath);
00277     BufferedInputStream bis = new BufferedInputStream(fis);
00278 
00279     ZipEntry fileEntry = new ZipEntry(path);
00280     zos.putNextEntry(fileEntry);
00281 
00282     byte[] data = new byte[1024];
00283     int byteCount;
00284     while ((byteCount = bis.read(data, 0, 1024)) > -1)
00285       zos.write(data, 0, byteCount);
00286 
00287     if (logger.isDebugEnabled())
00288       logger.debug(filePath);
00289   }
00290 
00291   /**
00292    * @return Returns the useCompression.
00293    */
00294   public boolean getUseCompression()
00295   {
00296     return useCompression;
00297   }
00298 
00299   /**
00300    * @param useCompression The useCompression to set.
00301    */
00302   public void setUseCompression(boolean useCompression)
00303   {
00304     this.useCompression = useCompression;
00305   }
00306 }

Generated on Mon Apr 11 22:01:36 2005 for C-JDBC by  doxygen 1.3.9.1