1 23 package com.sun.enterprise.diagnostics.util; 24 25 import com.sun.logging.LogDomains; 26 27 import java.nio.channels.*; 28 import java.io.*; 29 import java.util.*; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 import java.util.zip.*; 33 import java.util.jar.*; 34 35 38 public class FileUtils { 39 40 static final int BUFFER = 2048; 41 42 private static Logger logger = 43 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 44 45 52 public static void copyFile(String srcFile, String destFile) 53 throws IOException { 54 FileInputStream istream = new FileInputStream(srcFile); 55 File dest = new File(destFile); 56 copyFile(istream, dest); 57 } 58 59 67 public static void copyFile(InputStream istream, String destFile) 68 throws IOException { 69 File dest = new File(destFile); 70 copyFile(istream, dest); 71 } 72 73 81 public static void copyFile(FileInputStream istream, String destFile) 82 throws IOException { 83 File dest = new File(destFile); 84 copyFile(istream, dest); 85 } 86 87 95 public static void copyFile(InputStream istream, File dest) 96 throws IOException { 97 OutputStream ostream = new FileOutputStream(dest); 98 dest.createNewFile(); 99 copyFileToStream(istream, ostream); 100 ostream.close(); 101 } 102 103 111 public static void copyFile(FileInputStream istream, File dest) 112 throws IOException { 113 if (!dest.exists()) { 114 dest.getParentFile().mkdirs(); 115 } 116 FileOutputStream ostream = new FileOutputStream(dest); 117 dest.createNewFile(); 118 copyFileToStream(istream, ostream); 119 ostream.close(); 120 } 121 122 129 public static void copyFileToStream(InputStream istream, OutputStream ostream) 130 throws IOException { 131 while(true){ 132 int nextByte = istream.read(); 133 if (nextByte==-1) 134 break; 135 ostream.write(nextByte); 136 } 137 istream.close(); 138 } 139 140 147 public static void copyFileToStream(FileInputStream istream, FileOutputStream ostream) 148 throws IOException { 149 FileChannel srcChannel = istream.getChannel(); 150 FileChannel destChannel = ostream.getChannel(); 151 srcChannel.transferTo(0, srcChannel.size(), destChannel); 152 srcChannel.close(); 153 destChannel.close(); 154 istream.close(); 155 } 156 157 164 public static void copyFileToWriter(InputStream istream, Writer writer) 165 throws IOException { 166 while(true){ 167 int nextByte = istream.read(); 168 if (nextByte==-1) 169 break; 170 writer.write(nextByte); 171 } 172 istream.close(); 173 } 174 175 183 public static void copySearchPatternToFile(String srcFileName, 184 String dstFileName, String searchPattern) 185 throws FileNotFoundException, IOException { 186 BufferedReader inFile = new BufferedReader(new FileReader(srcFileName)); 187 PrintWriter out = new PrintWriter 188 (new BufferedWriter(new FileWriter(dstFileName))); 189 String fileLine; 190 while((fileLine = inFile.readLine()) != null) 191 if (fileLine.matches("(?i).*"+searchPattern+".*")) 192 out.println(fileLine); 193 if (inFile != null) 194 inFile.close(); 195 if (out != null) 196 out.close(); 197 } 198 199 207 public static void copyDir(String srcDir, String dstDir) 208 throws IOException { 209 copyDir(srcDir, dstDir, null, false); 210 } 211 212 222 public static void copyDir(String srcDir, String dstDir, boolean subDir) 223 throws IOException { 224 copyDir(srcDir, dstDir, null, subDir); 225 } 226 227 235 public static void copyDir(String srcDir, String dstDir, 236 FilenameFilter filter, boolean subDir) 237 throws IOException { 238 int numFiles = 0; 239 String [] strFiles = null; 240 241 if (filter == null) 242 strFiles = (new File(srcDir)).list(); 243 else 244 strFiles = (new File(srcDir)).list(filter); 245 246 if (strFiles != null) 247 numFiles = strFiles.length; 248 249 for (int i=0; i<numFiles; i++) { 250 String srcFile = srcDir+File.separator+strFiles[i]; 251 String dstFile = dstDir+File.separator+strFiles[i]; 252 if ((new File(srcFile)).isFile()) { 253 copyFile(srcFile,dstFile); 254 } else if(subDir) { 255 File dstSubDir = new File(dstFile); 256 dstSubDir.mkdirs(); 257 copyDir(srcFile,dstFile,filter,subDir); 258 } 259 } 260 } 261 262 268 public static void extractJarFiles(String dir, String dest){ 269 try{ 270 File aJarDir = new File(dir); 271 File files[] = aJarDir.listFiles(); 272 273 FilenameFilter filter = getFilenameFilter(".jar"); 274 275 for(File file : files) 276 { 277 if(filter.accept(aJarDir, file.getName())){ 279 String fileName = file.getName(); 280 File outputDir = new File(dest); 283 284 outputDir.mkdirs(); 285 286 287 unjar(file,"",outputDir.getAbsolutePath()); 288 289 file.delete(); 290 } 291 } 292 } 293 catch(Exception e){ 294 logger.log(Level.WARNING, e.getMessage(), e.fillInStackTrace()); 295 } 296 } 297 298 public static FilenameFilter getFilenameFilter(final String extension){ 299 FilenameFilter filter = new FilenameFilter() { 300 public boolean accept(File dir, String name) { 301 boolean result = false; 302 if(name !=null && extension !=null){ 303 result = name.toLowerCase().endsWith(extension.toLowerCase()); 304 } 305 return result; 306 } 307 }; 308 return filter; 309 } 310 318 public static void jarDirectory(File jarFile, String dir) { 319 320 try{ 321 322 323 BufferedInputStream origin = null; 324 File aJarDir = new File(dir); 325 File parent = jarFile.getParentFile(); 326 327 if(!parent.exists()) 328 parent.mkdirs(); 329 330 FileOutputStream dest = new FileOutputStream(jarFile); 331 JarOutputStream out = new JarOutputStream(new BufferedOutputStream(dest)); 332 out.setMethod(JarOutputStream.DEFLATED); 333 byte data[] = new byte[BUFFER]; 334 335 List files = FileUtils.getFileListing( aJarDir, true ); 336 337 Iterator filesIter = files.iterator(); 338 int length = dir.length() ; 339 341 while( filesIter.hasNext() ){ 342 File f = (File)filesIter.next(); 343 String path = f.getPath(); 344 String relativePath = path.substring(length); 345 if(relativePath.startsWith(""+File.separator)) 346 relativePath = path.substring(length +1); 347 348 if (f.isDirectory()) 349 relativePath = relativePath + "/"; 350 351 JarEntry entry = new JarEntry(relativePath); 353 out.putNextEntry(entry); 354 355 if (!f.isDirectory()){ 356 FileInputStream fi = new FileInputStream(path); 357 origin = new BufferedInputStream(fi, BUFFER); 358 359 int count; 360 while((count = origin.read(data, 0, BUFFER)) != -1) { 361 out.write(data, 0, count); 362 } 363 } 364 if(origin!=null) 365 try{origin.close();}catch(Exception e){e.printStackTrace();} 366 } 367 if(out!=null) 368 try{out.close();}catch(Exception e){e.printStackTrace();} 369 } catch (FileNotFoundException fnfEx){ 370 fnfEx.printStackTrace(); 371 } catch (IOException ioEx){ 372 ioEx.printStackTrace(); 373 } 374 } 375 376 384 public static void zipDirectory(File jarFile, String dir) { 385 try{ 386 BufferedInputStream origin = null; 387 File aJarDir = new File(dir); 388 FileOutputStream dest = new FileOutputStream(jarFile); 389 ZipOutputStream out = new ZipOutputStream( 390 new BufferedOutputStream(dest)); 391 out.setMethod(ZipOutputStream.DEFLATED); 392 byte data[] = new byte[BUFFER]; 393 394 List files = FileUtils.getFileListing( aJarDir, true ); 395 396 Iterator filesIter = files.iterator(); 397 while( filesIter.hasNext() ){ 398 File f = (File)filesIter.next(); 399 String path = f.getPath(); 400 if (f.isDirectory()) 401 path = path + "/"; 402 403 ZipEntry entry = new ZipEntry(path); 404 out.putNextEntry(entry); 405 406 if (!f.isDirectory()){ 407 FileInputStream fi = new FileInputStream(path); 408 origin = new BufferedInputStream(fi, BUFFER); 409 410 int count; 411 while((count = origin.read(data, 0, BUFFER)) != -1) { 412 out.write(data, 0, count); 413 } 414 } 415 if(origin!=null) 416 try{origin.close();}catch(Exception e){e.printStackTrace();} 417 } 418 if(out!=null) 419 try{out.close();}catch(Exception e){e.printStackTrace();} 420 } catch (FileNotFoundException fnfEx){ 421 fnfEx.printStackTrace(); 422 } catch (IOException ioEx){ 423 ioEx.printStackTrace(); 424 } 425 } 426 public static void unjar(File jarFile, String entryPath, String destDir) { 427 428 final int BUFFER = 2048; 429 try { 430 BufferedOutputStream dest; 431 BufferedInputStream is; 432 byte data[] = new byte[BUFFER]; 433 434 if (entryPath==null) 435 entryPath=""; 436 437 JarFile jarfile = new JarFile(jarFile); 438 Enumeration e = jarfile.entries(); 439 440 while(e.hasMoreElements()) { JarEntry entry = (JarEntry) e.nextElement(); 442 is = new BufferedInputStream(jarfile.getInputStream(entry)); 443 444 String path = entry.getName(); 445 if (path.startsWith(entryPath)) { 446 int start = path.lastIndexOf("/"); 447 String basis = null; 448 if(start != -1) { 449 basis = path.substring(0, start); 450 } 451 String filename = path.substring(start + 1); 452 File tmpPath = new File(destDir); 453 if(basis != null && basis.length() != 0){ 454 tmpPath = new File(tmpPath, basis); 455 tmpPath.mkdirs(); 456 } 457 458 if(filename != null && filename.length() != 0){ 459 File destfile = new File(tmpPath, filename); 460 FileOutputStream fos = new FileOutputStream(destfile); 461 dest = new BufferedOutputStream(fos, BUFFER); 462 463 int count; 464 while ((count = is.read(data, 0, BUFFER))!= -1) { 465 dest.write(data, 0, count); 466 } 467 dest.flush(); 468 dest.close(); 469 is.close(); 470 } 471 } 472 } 473 } catch(Exception e) { 474 e.printStackTrace(); 475 } 476 } 477 478 484 static public List getFileListing( File aStartingDir , boolean recurse) throws FileNotFoundException{ 485 validateDirectory(aStartingDir); 486 File[] filesAndDirs = aStartingDir.listFiles(); 487 List filesDirs = Arrays.asList(filesAndDirs); 488 489 if (!recurse) { 490 Collections.sort(filesDirs); 491 return filesDirs; 492 } 493 494 Iterator filesIter = filesDirs.iterator(); 495 List result = new ArrayList(); 496 File file = null; 497 while ( filesIter.hasNext() ) { 498 file = (File)filesIter.next(); 499 result.add(file); if (recurse && !file.isFile()) { 501 List deeperList = getFileListing(file, true); 504 result.addAll(deeperList); 505 } 506 507 } 508 Collections.sort(result); 509 return result; 510 } 511 512 521 static public List getFileListing(File aStartingDir, boolean recurse, 522 FilenameFilter nameFilter, Comparator comparator) 523 throws FileNotFoundException { 524 525 validateDirectory(aStartingDir); 526 527 File[] filesAndDirs = aStartingDir.listFiles(nameFilter); 528 List filesDirs = Arrays.asList(filesAndDirs); 529 530 if (!recurse) { 531 Collections.sort(filesDirs, comparator); 532 return filesDirs; 533 } 534 535 Iterator filesIter = filesDirs.iterator(); 536 List result = new ArrayList(); 537 File file = null; 538 while ( filesIter.hasNext() ) { 539 file = (File)filesIter.next(); 540 result.add(file); if (recurse && !file.isFile()) { 542 List deeperList = getFileListing(file,true); 545 result.addAll(deeperList); 546 } 547 548 } 549 Collections.sort(result, comparator); 550 return result; 551 } 552 553 556 static private void validateDirectory(File aDirectory) throws FileNotFoundException { 557 if (aDirectory == null) { 558 throw new IllegalArgumentException ("Directory should not be null."); 559 } 560 if (!aDirectory.exists()) { 561 throw new FileNotFoundException("Directory does not exist: " + aDirectory); 562 } 563 if (!aDirectory.isDirectory()) { 564 throw new IllegalArgumentException ("Is not a directory: " + aDirectory); 565 } 566 if (!aDirectory.canRead()) { 567 throw new IllegalArgumentException ("Directory cannot be read: " + aDirectory); 568 } 569 } 570 571 572 573 578 public static String getFileName(String absPath) { 579 File file = new File(absPath); 580 return file.getName(); 581 } 582 583 public static void deleteFile(String fileName){ 584 deleteFile(new File(fileName)); 585 } 586 587 public static void deleteFile(File file){ 588 if (file != null) { 589 if(file.isDirectory()){ 590 File[] files = file.listFiles(); 591 int size = files.length; 592 for(int i =0; i < size; i++){ 593 deleteFile(files[i]); 594 } 595 } 596 file.delete(); 597 } 598 } 599 600 public static void moveFile(String sourceFile, String destFile) 601 throws IOException { 602 copyFile(sourceFile, destFile); 603 new File(sourceFile).delete(); 604 } 605 } 606 | Popular Tags |