1 24 25 package org.objectweb.cjdbc.common.util; 26 27 import java.io.BufferedInputStream ; 28 import java.io.BufferedOutputStream ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.util.zip.ZipEntry ; 34 import java.util.zip.ZipInputStream ; 35 import java.util.zip.ZipOutputStream ; 36 37 import org.objectweb.cjdbc.common.i18n.Translate; 38 import org.objectweb.cjdbc.common.log.Trace; 39 40 47 public class Zipper 48 { 49 50 public static final String ZIP_EXT = ".zip"; 51 52 private static final int BUFFER_SIZE = 2048; 53 54 55 public static final int STORE_FULL_PATH_IN_ZIP = 0; 56 57 public static final int STORE_NAME_ONLY_IN_ZIP = 1; 58 59 public static final int STORE_RELATIVE_PATH_IN_ZIP = 2; 60 61 public static final int STORE_PATH_FROM_ZIP_ROOT = 3; 62 63 static Trace logger = Trace 64 .getLogger(Zipper.class 65 .getName()); 66 67 77 public static void zip(String zipName, String rootDir, int storePolicy) 78 throws Exception 79 { 80 if (zipName == null || rootDir == null) 81 throw new Exception ("Invalid arguments to create zip file"); 82 try 83 { 84 FileOutputStream fos = new FileOutputStream (zipName); 85 ZipOutputStream zos = new ZipOutputStream (fos); 86 87 directoryWalker(rootDir, rootDir, zos, storePolicy); 88 89 zos.flush(); 90 zos.finish(); 91 zos.close(); 92 fos.close(); 93 } 94 catch (IOException e) 95 { 96 logger.error(e.getMessage()); 97 throw e; 98 } 99 } 100 101 108 public static void unzip(String zipName, String targetDir) throws Exception 109 { 110 File ftargetDir = new File (targetDir); 111 if (!ftargetDir.exists()) 112 ftargetDir.mkdirs(); 113 if (!ftargetDir.exists()) 114 throw new Exception (Translate.get("zip.invalid.target.directory")); 115 116 File fzipname = new File (zipName); 117 if (!fzipname.exists()) 118 throw new Exception (Translate.get("zip.invalid.source.file", zipName)); 119 120 try 121 { 122 FileInputStream fis = new FileInputStream (fzipname); 123 ZipInputStream zis = new ZipInputStream (fis); 124 125 ZipEntry entry; 126 127 byte[] data = new byte[BUFFER_SIZE]; 128 while ((entry = zis.getNextEntry()) != null) 129 { 130 int count; 131 String target = targetDir + File.separator + entry.getName(); 132 File fget = new File (target); 133 fget.mkdirs(); fget.delete(); if (logger.isDebugEnabled()) 136 logger.debug(Translate.get("zip.extracting", new String []{ 137 String.valueOf(entry), fget.getAbsolutePath()})); 138 FileOutputStream fos = new FileOutputStream (target); 139 140 BufferedOutputStream dest = new BufferedOutputStream (fos, BUFFER_SIZE); 141 while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) 142 { 143 dest.write(data, 0, count); 144 } 145 dest.flush(); 146 dest.close(); 147 } 148 zis.close(); 149 } 150 catch (Exception e) 151 { 152 logger.error("Error while uncompressing archive", e); 153 throw e; 154 } 155 } 156 157 167 private static void directoryWalker(String currentDir, String rootDir, 168 ZipOutputStream zos, int storePolicy) throws IOException 169 { 170 File dirObj = new File (currentDir); 171 if (dirObj.exists() == true) 172 { 173 if (dirObj.isDirectory() == true) 174 { 175 176 File [] fileList = dirObj.listFiles(); 177 178 for (int i = 0; i < fileList.length; i++) 179 { 180 if (fileList[i].isDirectory()) 181 { 182 directoryWalker(fileList[i].getPath(), rootDir, zos, storePolicy); 183 } 184 else if (fileList[i].isFile()) 185 { 186 zipFile(fileList[i].getPath(), zos, storePolicy, rootDir); 187 } 188 } 189 } 190 else 191 { 192 if (logger.isDebugEnabled()) 193 logger.debug(Translate.get("zip.not.directory", rootDir)); 194 } 195 } 196 else 197 { 198 if (logger.isDebugEnabled()) 199 logger.debug(Translate.get("zip.directory.not.found", rootDir)); 200 } 201 } 202 203 212 private static void zipFile(String filePath, ZipOutputStream zos, 213 int storePolicy, String rootDir) throws IOException 214 { 215 File ffilePath = new File (filePath); 216 String path = ""; 217 switch (storePolicy) 218 { 219 case STORE_FULL_PATH_IN_ZIP : 220 path = ffilePath.getAbsolutePath(); 221 break; 222 case STORE_NAME_ONLY_IN_ZIP : 223 ffilePath.getName(); 224 break; 225 case STORE_RELATIVE_PATH_IN_ZIP : 226 File f = new File (""); 227 String pathToHere = f.getAbsolutePath(); 228 path = ffilePath.getAbsolutePath(); 229 path = path.substring(path.indexOf(pathToHere + File.separator) 230 + pathToHere.length()); 231 break; 232 case STORE_PATH_FROM_ZIP_ROOT : 233 path = ffilePath.getAbsolutePath(); 234 String tmpDir = rootDir + File.separator; 235 path = path.substring(path.indexOf(tmpDir) + tmpDir.length()); 237 break; 238 default : 239 break; 240 } 241 242 if (logger.isDebugEnabled()) 243 logger 244 .debug(Translate.get("zip.archiving", new String []{filePath, path})); 245 246 FileInputStream fileStream = new FileInputStream (filePath); 247 BufferedInputStream bis = new BufferedInputStream (fileStream); 248 249 ZipEntry fileEntry = new ZipEntry (path); 250 zos.putNextEntry(fileEntry); 251 252 byte[] data = new byte[BUFFER_SIZE]; 253 int byteCount; 254 while ((byteCount = bis.read(data, 0, BUFFER_SIZE)) > -1) 255 zos.write(data, 0, byteCount); 256 } 257 258 } | Popular Tags |