1 23 24 27 28 package com.sun.enterprise.config.backup.util; 29 30 import java.io.*; 31 import java.util.*; 32 import java.util.zip.*; 33 34 35 public class ZipWriter 36 { 37 public ZipWriter(String zipFilename, String dirName) throws ZipFileException 38 { 39 init(zipFilename, dirName); 40 createItemList(null); 41 } 42 43 45 public ZipWriter(String zipFilename, String dirName, ZipItem[] theItems) throws ZipFileException 46 { 47 items = theItems; 48 init(zipFilename, dirName); 49 } 50 51 53 public ZipWriter(String zipFilename, String dirName, String [] fileList) throws ZipFileException 54 { 55 init(zipFilename, dirName); 56 createItemList(fileList); 57 } 58 59 61 public ZipWriter(OutputStream outStream, String dirName, String [] fileList) throws ZipFileException 62 { 63 init(outStream, dirName); 64 createItemList(fileList); 65 } 66 67 74 75 public void excludeDirs(String [] dirs) 76 { 77 for(int i = 0; i < dirs.length; i++) 79 { 80 if(!dirs[i].endsWith("/")) 81 dirs[i] += "/"; 82 } 83 84 List list = new ArrayList(items.length); 86 87 for(int i = 0; i < items.length; i++) 88 { 89 for(int j = 0; j < dirs.length; j++) 90 { 91 if(!items[i].name.startsWith(dirs[j])) 92 list.add(items[i]); 93 } 96 } 97 98 if(list.size() != items.length) 100 { 101 items = new ZipItem[list.size()]; 102 items = (ZipItem[])list.toArray(items); 103 } 104 } 105 106 108 private void init(String outFileName, String dirName) throws ZipFileException 109 { 110 try 111 { 112 init(new FileOutputStream(outFileName), dirName); 113 } 114 catch(Exception e) 115 { 116 throw new ZipFileException(e); 117 } 118 } 119 120 122 private void init(OutputStream outStream, String dirName) throws ZipFileException 123 { 124 try 125 { 126 File f = new File(dirName); 128 129 dirName = FileUtils.safeGetCanonicalPath(f); 131 dirName = dirName.replace('\\', '/'); 133 134 if(!dirName.endsWith("/")) 136 dirName += "/"; 137 138 139 this.dirName = dirName; 140 zipStream = new ZipOutputStream(outStream); 141 } 142 catch(Throwable t) 143 { 144 throw new ZipFileException(t); 145 } 146 } 147 148 150 155 public void safeWrite() throws ZipFileException 156 { 157 try 158 { 159 for(int i = 0; i < items.length; i++) 160 { 161 try 162 { 163 addEntry(items[i]); 164 } 165 catch (ZipException e) 166 { 167 } 169 } 170 171 zipStream.close(); 172 } 173 catch(ZipFileException z) 174 { 175 throw z; 176 } 177 catch(Exception e) 178 { 179 throw new ZipFileException(e); 180 } 181 } 182 183 185 public void write() throws ZipFileException 186 { 187 try 188 { 189 for(int i = 0; i < items.length; i++) 190 { 191 addEntry(items[i]); 192 } 193 194 zipStream.close(); 195 } 196 catch(ZipFileException z) 197 { 198 throw z; 199 } 200 catch(Exception e) 201 { 202 throw new ZipFileException(e); 203 } 204 } 205 206 208 private void addEntry(ZipItem item) throws ZipFileException, IOException 209 { 210 int totalBytes = 0; 211 ZipEntry ze = new ZipEntry(item.name); 212 213 zipStream.putNextEntry(ze); 214 215 if(!item.name.endsWith("/")) 221 { 222 FileInputStream in = new FileInputStream(item.file); 223 224 for(int numBytes = in.read(buffer); numBytes > 0; numBytes = in.read(buffer)) 225 { 226 zipStream.write(buffer, 0, numBytes); 227 totalBytes += numBytes; 228 } 229 230 in.close(); 231 } 232 zipStream.closeEntry(); 233 } 234 235 237 private void createItemList(String [] files) throws ZipFileException 238 { 239 try 240 { 241 if(files == null) 242 { 243 FileListerRelative lister = new FileListerRelative(new File(dirName)); 244 files = lister.getFiles(); 245 } 246 247 if(files.length <= 0) 248 throw new ZipFileException("No files to add!"); 249 250 items = new ZipItem[files.length]; 251 252 for(int i = 0; i < files.length; i++) 253 { 254 File f = new File(dirName + files[i]); 255 items[i] = new ZipItem(f, files[i].replace('\\', '/')); 257 if(f.isDirectory()) 259 items[i].name += "/"; 260 } 261 } 262 catch(Throwable t) 263 { 264 throw new ZipFileException(t); 265 } 266 267 } 268 269 271 String getDirName() 272 { 273 return dirName; 274 } 275 276 278 private static void usage() 279 { 280 System.out.println("usage: java com.elf.util.zip.ZipWriter zip-filename directory-name"); 281 System.exit(1); 282 } 283 284 286 public static void main(String [] args) 287 { 288 289 if(args == null || args.length != 2) 290 usage(); 291 292 try 293 { 294 ZipWriter zw = new ZipWriter(args[0], args[1]); 295 zw.write(); 296 } 297 catch(ZipFileException e) 298 { 299 System.exit(0); 300 } 301 } 302 303 305 private String dirName = null; 307 private ZipOutputStream zipStream = null; 308 private byte[] buffer = new byte[16384]; 309 private ZipItem[] items = null; 310 } 311 | Popular Tags |