00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00042
00043
00044
00045 public class ZipMe
00046 {
00047
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
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
00073
00074 public ZipMe()
00075 {
00076 }
00077
00078
00079
00080
00081
00082
00083
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
00112
00113
00114
00115
00116
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
00126
00127
00128
00129
00130
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
00140
00141
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
00154
00155
00156
00157
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();
00186 fget.delete();
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
00293
00294 public boolean getUseCompression()
00295 {
00296 return useCompression;
00297 }
00298
00299
00300
00301
00302 public void setUseCompression(boolean useCompression)
00303 {
00304 this.useCompression = useCompression;
00305 }
00306 }