1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.BufferedInputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.zip.GZIPInputStream ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.types.EnumeratedAttribute; 31 import org.apache.tools.ant.types.Resource; 32 import org.apache.tools.ant.util.FileNameMapper; 33 import org.apache.tools.ant.util.FileUtils; 34 import org.apache.tools.bzip2.CBZip2InputStream; 35 import org.apache.tools.tar.TarEntry; 36 import org.apache.tools.tar.TarInputStream; 37 38 39 40 57 public class Untar extends Expand { 58 61 private UntarCompressionMethod compression = new UntarCompressionMethod(); 62 63 75 public void setCompression(UntarCompressionMethod method) { 76 compression = method; 77 } 78 79 85 public void setEncoding(String encoding) { 86 throw new BuildException("The " + getTaskName() 87 + " task doesn't support the encoding" 88 + " attribute", getLocation()); 89 } 90 91 94 95 protected void expandFile(FileUtils fileUtils, File srcF, File dir) { 96 FileInputStream fis = null; 97 try { 98 fis = new FileInputStream (srcF); 99 expandStream(srcF.getPath(), fis, dir); 100 } catch (IOException ioe) { 101 throw new BuildException("Error while expanding " + srcF.getPath(), 102 ioe, getLocation()); 103 } finally { 104 FileUtils.close(fis); 105 } 106 } 107 108 115 protected void expandResource(Resource srcR, File dir) { 116 InputStream i = null; 117 try { 118 i = srcR.getInputStream(); 119 expandStream(srcR.getName(), i, dir); 120 } catch (IOException ioe) { 121 throw new BuildException("Error while expanding " + srcR.getName(), 122 ioe, getLocation()); 123 } finally { 124 FileUtils.close(i); 125 } 126 } 127 128 131 private void expandStream(String name, InputStream stream, File dir) 132 throws IOException { 133 TarInputStream tis = null; 134 try { 135 tis = 136 new TarInputStream(compression.decompress(name, 137 new BufferedInputStream (stream))); 138 log("Expanding: " + name + " into " + dir, Project.MSG_INFO); 139 TarEntry te = null; 140 FileNameMapper mapper = getMapper(); 141 while ((te = tis.getNextEntry()) != null) { 142 extractFile(FileUtils.getFileUtils(), null, dir, tis, 143 te.getName(), te.getModTime(), 144 te.isDirectory(), mapper); 145 } 146 log("expand complete", Project.MSG_VERBOSE); 147 } finally { 148 FileUtils.close(tis); 149 } 150 } 151 152 156 public static final class UntarCompressionMethod 157 extends EnumeratedAttribute { 158 159 163 private static final String NONE = "none"; 164 167 private static final String GZIP = "gzip"; 168 171 private static final String BZIP2 = "bzip2"; 172 173 174 177 public UntarCompressionMethod() { 178 super(); 179 setValue(NONE); 180 } 181 182 187 public String [] getValues() { 188 return new String [] {NONE, GZIP, BZIP2}; 189 } 190 191 202 public InputStream decompress(final String name, 203 final InputStream istream) 204 throws IOException , BuildException { 205 final String v = getValue(); 206 if (GZIP.equals(v)) { 207 return new GZIPInputStream (istream); 208 } else { 209 if (BZIP2.equals(v)) { 210 final char[] magic = new char[] {'B', 'Z'}; 211 for (int i = 0; i < magic.length; i++) { 212 if (istream.read() != magic[i]) { 213 throw new BuildException( 214 "Invalid bz2 file." + name); 215 } 216 } 217 return new CBZip2InputStream(istream); 218 } 219 } 220 return istream; 221 } 222 } 223 } 224 | Popular Tags |