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