1 18 19 package org.apache.tools.ant.taskdefs; 20 21 22 import java.io.BufferedInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.util.FileUtils; 28 import org.apache.tools.bzip2.CBZip2InputStream; 29 30 39 40 public class BUnzip2 extends Unpack { 41 42 private static final String DEFAULT_EXTENSION = ".bz2"; 43 44 48 protected String getDefaultExtension() { 49 return DEFAULT_EXTENSION; 50 } 51 52 55 protected void extract() { 56 if (source.lastModified() > dest.lastModified()) { 57 log("Expanding " + source.getAbsolutePath() + " to " 58 + dest.getAbsolutePath()); 59 60 FileOutputStream out = null; 61 CBZip2InputStream zIn = null; 62 InputStream fis = null; 63 BufferedInputStream bis = null; 64 try { 65 out = new FileOutputStream (dest); 66 fis = srcResource.getInputStream(); 67 bis = new BufferedInputStream (fis); 68 int b = bis.read(); 69 if (b != 'B') { 70 throw new BuildException("Invalid bz2 file.", getLocation()); 71 } 72 b = bis.read(); 73 if (b != 'Z') { 74 throw new BuildException("Invalid bz2 file.", getLocation()); 75 } 76 zIn = new CBZip2InputStream(bis); 77 byte[] buffer = new byte[8 * 1024]; 78 int count = 0; 79 do { 80 out.write(buffer, 0, count); 81 count = zIn.read(buffer, 0, buffer.length); 82 } while (count != -1); 83 } catch (IOException ioe) { 84 String msg = "Problem expanding bzip2 " + ioe.getMessage(); 85 throw new BuildException(msg, ioe, getLocation()); 86 } finally { 87 FileUtils.close(bis); 88 FileUtils.close(fis); 89 FileUtils.close(out); 90 FileUtils.close(zIn); 91 } 92 } 93 } 94 95 106 protected boolean supportsNonFileResources() { 107 return getClass().equals(BUnzip2.class); 108 } 109 } 110 | Popular Tags |