src/org/objectweb/cjdbc/common/util/ZipMe.java

説明を見る。
00001 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.Deflater; 00034 import java.util.zip.DeflaterOutputStream; 00035 import java.util.zip.InflaterInputStream; 00036 import java.util.zip.ZipEntry; 00037 import java.util.zip.ZipInputStream; 00038 import java.util.zip.ZipOutputStream; 00039 00040 import org.objectweb.cjdbc.common.i18n.Translate; 00041 import org.objectweb.cjdbc.common.log.Trace; 00042 00048 public class ZipMe 00049 { 00050 // Buffers and streams 00051 private FileOutputStream fos = null; 00052 private ZipOutputStream zos = null; 00053 private ZipInputStream zis = null; 00054 private FileInputStream fis = null; 00055 private BufferedOutputStream dest; 00056 private String rootDir; 00057 private String zipPath; 00058 private boolean useCompression = true; 00059 00060 static final int BUFFER = 2048; 00062 public static final String ZIP_EXT = ".cjdbc"; 00063 00064 // Store policy 00065 static final int STORE_FULL_PATH_IN_ZIP = 0; 00066 static final int STORE_NAME_ONLY_IN_ZIP = 1; 00067 static final int STORE_RELATIVE_PATH_IN_ZIP = 2; 00068 static final int STORE_PATH_FROM_ZIP_ROOT = 3; 00069 static final int STORE_POLICY = STORE_PATH_FROM_ZIP_ROOT; 00070 00071 static Trace logger = Trace 00072 .getLogger(ZipMe.class 00073 .getName()); 00074 00078 public ZipMe() 00079 { 00080 } 00081 00089 public void create(String zipname, String rootdir) throws Exception 00090 { 00091 if (zipname == null || rootdir == null) 00092 throw new Exception("Invalid arguments to create zip file"); 00093 this.rootDir = rootdir; 00094 try 00095 { 00096 zipPath = zipname + ZIP_EXT; 00097 fos = new FileOutputStream(zipPath); 00098 if (useCompression) 00099 zos = new ZipOutputStream(new DeflaterOutputStream(fos, new Deflater( 00100 Deflater.BEST_COMPRESSION))); 00101 else 00102 zos = new ZipOutputStream(fos); 00103 00104 dirFunc(rootdir); 00105 00106 zos.flush(); 00107 zos.close(); 00108 fos.close(); 00109 } 00110 catch (IOException e) 00111 { 00112 logger.error(e.getMessage()); 00113 throw e; 00114 } 00115 } 00116 00125 public void create(String zipname, String rootdir, String putMeThere) 00126 throws Exception 00127 { 00128 create(putMeThere + File.separator + zipname, rootdir); 00129 } 00130 00139 public void expand(String zipname, String targetdir, String getMeThere) 00140 throws Exception 00141 { 00142 expand(getMeThere + File.separator + zipname, targetdir); 00143 } 00144 00150 public long getZipSize() 00151 { 00152 File f = new File(zipPath); 00153 if (f.exists()) 00154 return f.length(); 00155 else 00156 return -1; 00157 } 00158 00166 public void expand(String zipname, String targetdir) throws Exception 00167 { 00168 File ftargetDir = new File(targetdir); 00169 if (!ftargetDir.exists()) 00170 ftargetDir.mkdirs(); 00171 if (!ftargetDir.exists()) 00172 throw new Exception(Translate.get("zip.invalid.target.directory")); 00173 00174 zipPath = zipname + ZIP_EXT; 00175 File fzipname = new File(zipPath); 00176 if (!fzipname.exists()) 00177 throw new Exception(Translate.get("zip.invalid.source.file")); 00178 00179 try 00180 { 00181 fis = new FileInputStream(fzipname); 00182 if (useCompression) 00183 zis = new ZipInputStream(new InflaterInputStream(fis)); 00184 else 00185 zis = new ZipInputStream(new BufferedInputStream(fis)); 00186 00187 ZipEntry entry; 00188 00189 byte data[] = new byte[BUFFER]; 00190 while ((entry = zis.getNextEntry()) != null) 00191 { 00192 int count; 00193 String target = targetdir + File.separator + entry.getName(); 00194 File fget = new File(target); 00195 fget.mkdirs(); // create needed new directories 00196 fget.delete(); // delete directory but not parents 00197 if (logger.isDebugEnabled()) 00198 logger.debug(Translate.get("zip.extracting", new String[]{ 00199 String.valueOf(entry), fget.getAbsolutePath()})); 00200 fos = new FileOutputStream(target); 00201 00202 dest = new BufferedOutputStream(fos, BUFFER); 00203 while ((count = zis.read(data, 0, BUFFER)) != -1) 00204 { 00205 dest.write(data, 0, count); 00206 } 00207 dest.flush(); 00208 dest.close(); 00209 } 00210 zis.close(); 00211 } 00212 catch (Exception e) 00213 { 00214 e.printStackTrace(); 00215 logger.error(e.getMessage()); 00216 throw e; 00217 } 00218 } 00219 00220 private void dirFunc(String dirName) throws IOException 00221 { 00222 File dirObj = new File(dirName); 00223 if (dirObj.exists() == true) 00224 { 00225 if (dirObj.isDirectory() == true) 00226 { 00227 00228 File[] fileList = dirObj.listFiles(); 00229 00230 for (int i = 0; i < fileList.length; i++) 00231 { 00232 if (fileList[i].isDirectory()) 00233 { 00234 dirFunc(fileList[i].getPath()); 00235 } 00236 else if (fileList[i].isFile()) 00237 { 00238 00239 zipFunc(fileList[i].getPath()); 00240 } 00241 } 00242 } 00243 else 00244 { 00245 if (logger.isDebugEnabled()) 00246 logger.debug(Translate.get("zip.not.directory", dirName)); 00247 } 00248 } 00249 else 00250 { 00251 if (logger.isDebugEnabled()) 00252 logger.debug(Translate.get("zip.directory.not.found", dirName)); 00253 } 00254 } 00255 00256 private void zipFunc(String filePath) throws IOException 00257 { 00258 File ffilePath = new File(filePath); 00259 String path = ""; 00260 switch (STORE_POLICY) 00261 { 00262 case STORE_FULL_PATH_IN_ZIP : 00263 path = ffilePath.getAbsolutePath(); 00264 break; 00265 case STORE_NAME_ONLY_IN_ZIP : 00266 ffilePath.getName(); 00267 break; 00268 case STORE_RELATIVE_PATH_IN_ZIP : 00269 File f = new File(""); 00270 String pathToHere = f.getAbsolutePath(); 00271 path = ffilePath.getAbsolutePath(); 00272 path = path.substring(path.indexOf(pathToHere + File.separator) 00273 + pathToHere.length()); 00274 break; 00275 case STORE_PATH_FROM_ZIP_ROOT : 00276 path = ffilePath.getAbsolutePath(); 00277 String tmpDir = File.separator + rootDir + File.separator; 00278 path = rootDir + File.separator 00279 + path.substring(path.indexOf(tmpDir) + tmpDir.length()); 00280 logger.debug(path); 00281 break; 00282 default : 00283 break; 00284 } 00285 00286 FileInputStream fis = new FileInputStream(filePath); 00287 BufferedInputStream bis = new BufferedInputStream(fis); 00288 00289 ZipEntry fileEntry = new ZipEntry(path); 00290 zos.putNextEntry(fileEntry); 00291 00292 byte[] data = new byte[1024]; 00293 int byteCount; 00294 while ((byteCount = bis.read(data, 0, 1024)) > -1) 00295 { 00296 zos.write(data, 0, byteCount); 00297 } 00298 00299 logger.debug(filePath); 00300 } 00301 00305 public boolean getUseCompression() 00306 { 00307 return useCompression; 00308 } 00309 00313 public void setUseCompression(boolean useCompression) 00314 { 00315 this.useCompression = useCompression; 00316 } 00317 }

CJDBCversion1.0.4に対してTue Oct 12 15:15:58 2004に生成されました。 doxygen 1.3.8