KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Untar


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.util.zip.GZIPInputStream JavaDoc;
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 /**
41  * Untar a file.
42  * <p>For JDK 1.1 &quot;last modified time&quot; field is set to current time instead of being
43  * carried from the archive file.</p>
44  * <p>PatternSets are used to select files to extract
45  * <I>from</I> the archive. If no patternset is used, all files are extracted.
46  * </p>
47  * <p>FileSet>s may be used to select archived files
48  * to perform unarchival upon.
49  * </p>
50  * <p>File permissions will not be restored on extracted files.</p>
51  * <p>The untar task recognizes the long pathname entries used by GNU tar.<p>
52  *
53  * @since Ant 1.1
54  *
55  * @ant.task category="packaging"
56  */

57 public class Untar extends Expand {
58     /**
59      * compression method
60      */

61     private UntarCompressionMethod compression = new UntarCompressionMethod();
62
63     /**
64      * Set decompression algorithm to use; default=none.
65      *
66      * Allowable values are
67      * <ul>
68      * <li>none - no compression
69      * <li>gzip - Gzip compression
70      * <li>bzip2 - Bzip2 compression
71      * </ul>
72      *
73      * @param method compression method
74      */

75     public void setCompression(UntarCompressionMethod method) {
76         compression = method;
77     }
78
79     /**
80      * No encoding support in Untar.
81      * @param encoding not used
82      * @throws BuildException always
83      * @since Ant 1.6
84      */

85     public void setEncoding(String JavaDoc encoding) {
86         throw new BuildException("The " + getTaskName()
87                                  + " task doesn't support the encoding"
88                                  + " attribute", getLocation());
89     }
90
91     /**
92      * @see Expand#expandFile(FileUtils, File, File)
93      */

94     /** {@inheritDoc} */
95     protected void expandFile(FileUtils fileUtils, File JavaDoc srcF, File JavaDoc dir) {
96         FileInputStream JavaDoc fis = null;
97         try {
98             fis = new FileInputStream JavaDoc(srcF);
99             expandStream(srcF.getPath(), fis, dir);
100         } catch (IOException JavaDoc ioe) {
101             throw new BuildException("Error while expanding " + srcF.getPath(),
102                                      ioe, getLocation());
103         } finally {
104             FileUtils.close(fis);
105         }
106     }
107
108     /**
109      * This method is to be overridden by extending unarchival tasks.
110      *
111      * @param srcR the source resource
112      * @param dir the destination directory
113      * @since Ant 1.7
114      */

115     protected void expandResource(Resource srcR, File JavaDoc dir) {
116         InputStream JavaDoc i = null;
117         try {
118             i = srcR.getInputStream();
119             expandStream(srcR.getName(), i, dir);
120         } catch (IOException JavaDoc ioe) {
121             throw new BuildException("Error while expanding " + srcR.getName(),
122                                      ioe, getLocation());
123         } finally {
124             FileUtils.close(i);
125         }
126     }
127
128     /**
129      * @since Ant 1.7
130      */

131     private void expandStream(String JavaDoc name, InputStream JavaDoc stream, File JavaDoc dir)
132         throws IOException JavaDoc {
133         TarInputStream tis = null;
134         try {
135             tis =
136                 new TarInputStream(compression.decompress(name,
137                                                           new BufferedInputStream JavaDoc(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     /**
153      * Valid Modes for Compression attribute to Untar Task
154      *
155      */

156     public static final class UntarCompressionMethod
157         extends EnumeratedAttribute {
158
159         // permissible values for compression attribute
160
/**
161          * No compression
162          */

163         private static final String JavaDoc NONE = "none";
164         /**
165          * GZIP compression
166          */

167         private static final String JavaDoc GZIP = "gzip";
168         /**
169          * BZIP2 compression
170          */

171         private static final String JavaDoc BZIP2 = "bzip2";
172
173
174         /**
175          * Constructor
176          */

177         public UntarCompressionMethod() {
178             super();
179             setValue(NONE);
180         }
181
182         /**
183          * Get valid enumeration values
184          *
185          * @return valid values
186          */

187         public String JavaDoc[] getValues() {
188             return new String JavaDoc[] {NONE, GZIP, BZIP2};
189         }
190
191         /**
192          * This method wraps the input stream with the
193          * corresponding decompression method
194          *
195          * @param name provides location information for BuildException
196          * @param istream input stream
197          * @return input stream with on-the-fly decompression
198          * @exception IOException thrown by GZIPInputStream constructor
199          * @exception BuildException thrown if bzip stream does not
200          * start with expected magic values
201          */

202         public InputStream JavaDoc decompress(final String JavaDoc name,
203                                        final InputStream JavaDoc istream)
204             throws IOException JavaDoc, BuildException {
205             final String JavaDoc v = getValue();
206             if (GZIP.equals(v)) {
207                 return new GZIPInputStream JavaDoc(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