1 | /*
|
---|
2 | * Copyright 2000-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.BufferedInputStream;
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.FileInputStream;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.InputStream;
|
---|
25 | import java.util.zip.GZIPInputStream;
|
---|
26 | import org.apache.tools.ant.BuildException;
|
---|
27 | import org.apache.tools.ant.Project;
|
---|
28 | import org.apache.tools.ant.types.EnumeratedAttribute;
|
---|
29 | import org.apache.tools.ant.util.FileUtils;
|
---|
30 | import org.apache.tools.bzip2.CBZip2InputStream;
|
---|
31 | import org.apache.tools.tar.TarEntry;
|
---|
32 | import org.apache.tools.tar.TarInputStream;
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Untar a file.
|
---|
38 | * <p>For JDK 1.1 "last modified time" field is set to current time instead of being
|
---|
39 | * carried from the archive file.</p>
|
---|
40 | * <p>PatternSets are used to select files to extract
|
---|
41 | * <I>from</I> the archive. If no patternset is used, all files are extracted.
|
---|
42 | * </p>
|
---|
43 | * <p>FileSet>s may be used to select archived files
|
---|
44 | * to perform unarchival upon.
|
---|
45 | * </p>
|
---|
46 | * <p>File permissions will not be restored on extracted files.</p>
|
---|
47 | * <p>The untar task recognizes the long pathname entries used by GNU tar.<p>
|
---|
48 | *
|
---|
49 | * @since Ant 1.1
|
---|
50 | *
|
---|
51 | * @ant.task category="packaging"
|
---|
52 | */
|
---|
53 | public class Untar extends Expand {
|
---|
54 | /**
|
---|
55 | * compression method
|
---|
56 | */
|
---|
57 | private UntarCompressionMethod compression = new UntarCompressionMethod();
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Set decompression algorithm to use; default=none.
|
---|
61 | *
|
---|
62 | * Allowable values are
|
---|
63 | * <ul>
|
---|
64 | * <li>none - no compression
|
---|
65 | * <li>gzip - Gzip compression
|
---|
66 | * <li>bzip2 - Bzip2 compression
|
---|
67 | * </ul>
|
---|
68 | *
|
---|
69 | * @param method compression method
|
---|
70 | */
|
---|
71 | public void setCompression(UntarCompressionMethod method) {
|
---|
72 | compression = method;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * No encoding support in Untar.
|
---|
77 | * @param encoding not used
|
---|
78 | * @throws BuildException always
|
---|
79 | * @since Ant 1.6
|
---|
80 | */
|
---|
81 | public void setEncoding(String encoding) {
|
---|
82 | throw new BuildException("The " + getTaskName()
|
---|
83 | + " task doesn't support the encoding"
|
---|
84 | + " attribute", getLocation());
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * @see Expand#expandFile(FileUtils, File, File)
|
---|
89 | */
|
---|
90 | protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
|
---|
91 | FileInputStream fis = null;
|
---|
92 | TarInputStream tis = null;
|
---|
93 | try {
|
---|
94 | log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
|
---|
95 | fis = new FileInputStream(srcF);
|
---|
96 | tis = new TarInputStream(
|
---|
97 | compression.decompress(srcF, new BufferedInputStream(fis)));
|
---|
98 | TarEntry te = null;
|
---|
99 |
|
---|
100 | while ((te = tis.getNextEntry()) != null) {
|
---|
101 | extractFile(fileUtils, srcF, dir, tis,
|
---|
102 | te.getName(), te.getModTime(), te.isDirectory());
|
---|
103 | }
|
---|
104 | log("expand complete", Project.MSG_VERBOSE);
|
---|
105 |
|
---|
106 | } catch (IOException ioe) {
|
---|
107 | throw new BuildException("Error while expanding " + srcF.getPath(),
|
---|
108 | ioe, getLocation());
|
---|
109 | } finally {
|
---|
110 | if (tis != null) {
|
---|
111 | try {
|
---|
112 | tis.close();
|
---|
113 | } catch (IOException e) {
|
---|
114 | // ignore
|
---|
115 | }
|
---|
116 | } else if (fis != null) {
|
---|
117 | try {
|
---|
118 | fis.close();
|
---|
119 | } catch (IOException e) {
|
---|
120 | // ignore
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Valid Modes for Compression attribute to Untar Task
|
---|
128 | *
|
---|
129 | */
|
---|
130 | public static final class UntarCompressionMethod
|
---|
131 | extends EnumeratedAttribute {
|
---|
132 |
|
---|
133 | // permissible values for compression attribute
|
---|
134 | /**
|
---|
135 | * No compression
|
---|
136 | */
|
---|
137 | private static final String NONE = "none";
|
---|
138 | /**
|
---|
139 | * GZIP compression
|
---|
140 | */
|
---|
141 | private static final String GZIP = "gzip";
|
---|
142 | /**
|
---|
143 | * BZIP2 compression
|
---|
144 | */
|
---|
145 | private static final String BZIP2 = "bzip2";
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Constructor
|
---|
150 | */
|
---|
151 | public UntarCompressionMethod() {
|
---|
152 | super();
|
---|
153 | setValue(NONE);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Get valid enumeration values
|
---|
158 | *
|
---|
159 | * @return valid values
|
---|
160 | */
|
---|
161 | public String[] getValues() {
|
---|
162 | return new String[] {NONE, GZIP, BZIP2};
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * This method wraps the input stream with the
|
---|
167 | * corresponding decompression method
|
---|
168 | *
|
---|
169 | * @param file provides location information for BuildException
|
---|
170 | * @param istream input stream
|
---|
171 | * @return input stream with on-the-fly decompression
|
---|
172 | * @exception IOException thrown by GZIPInputStream constructor
|
---|
173 | * @exception BuildException thrown if bzip stream does not
|
---|
174 | * start with expected magic values
|
---|
175 | */
|
---|
176 | private InputStream decompress(final File file,
|
---|
177 | final InputStream istream)
|
---|
178 | throws IOException, BuildException {
|
---|
179 | final String value = getValue();
|
---|
180 | if (GZIP.equals(value)) {
|
---|
181 | return new GZIPInputStream(istream);
|
---|
182 | } else {
|
---|
183 | if (BZIP2.equals(value)) {
|
---|
184 | final char[] magic = new char[] {'B', 'Z'};
|
---|
185 | for (int i = 0; i < magic.length; i++) {
|
---|
186 | if (istream.read() != magic[i]) {
|
---|
187 | throw new BuildException(
|
---|
188 | "Invalid bz2 file." + file.toString());
|
---|
189 | }
|
---|
190 | }
|
---|
191 | return new CBZip2InputStream(istream);
|
---|
192 | }
|
---|
193 | }
|
---|
194 | return istream;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|