1 | /*
|
---|
2 | * Copyright 2000-2004 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.File;
|
---|
21 | import java.io.FileOutputStream;
|
---|
22 | import java.io.FileNotFoundException;
|
---|
23 | import java.io.InputStream;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.util.Date;
|
---|
26 | import java.util.Enumeration;
|
---|
27 | import java.util.Vector;
|
---|
28 | import org.apache.tools.ant.BuildException;
|
---|
29 | import org.apache.tools.ant.DirectoryScanner;
|
---|
30 | import org.apache.tools.ant.Project;
|
---|
31 | import org.apache.tools.ant.Task;
|
---|
32 | import org.apache.tools.ant.types.FileSet;
|
---|
33 | import org.apache.tools.ant.types.PatternSet;
|
---|
34 | import org.apache.tools.ant.types.selectors.SelectorUtils;
|
---|
35 | import org.apache.tools.ant.util.FileUtils;
|
---|
36 | import org.apache.tools.zip.ZipEntry;
|
---|
37 | import org.apache.tools.zip.ZipFile;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Unzip a file.
|
---|
41 | *
|
---|
42 | *
|
---|
43 | * @since Ant 1.1
|
---|
44 | *
|
---|
45 | * @ant.task category="packaging"
|
---|
46 | * name="unzip"
|
---|
47 | * name="unjar"
|
---|
48 | * name="unwar"
|
---|
49 | */
|
---|
50 | public class Expand extends Task {
|
---|
51 | private File dest; //req
|
---|
52 | private File source; // req
|
---|
53 | private boolean overwrite = true;
|
---|
54 | private Vector patternsets = new Vector();
|
---|
55 | private Vector filesets = new Vector();
|
---|
56 |
|
---|
57 | private static final String NATIVE_ENCODING = "native-encoding";
|
---|
58 |
|
---|
59 | private String encoding = "UTF8";
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Do the work.
|
---|
63 | *
|
---|
64 | * @exception BuildException Thrown in unrecoverable error.
|
---|
65 | */
|
---|
66 | public void execute() throws BuildException {
|
---|
67 | if ("expand".equals(getTaskType())) {
|
---|
68 | log("!! expand is deprecated. Use unzip instead. !!");
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (source == null && filesets.size() == 0) {
|
---|
72 | throw new BuildException("src attribute and/or filesets must be "
|
---|
73 | + "specified");
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (dest == null) {
|
---|
77 | throw new BuildException(
|
---|
78 | "Dest attribute must be specified");
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (dest.exists() && !dest.isDirectory()) {
|
---|
82 | throw new BuildException("Dest must be a directory.", getLocation());
|
---|
83 | }
|
---|
84 |
|
---|
85 | FileUtils fileUtils = FileUtils.newFileUtils();
|
---|
86 |
|
---|
87 | if (source != null) {
|
---|
88 | if (source.isDirectory()) {
|
---|
89 | throw new BuildException("Src must not be a directory."
|
---|
90 | + " Use nested filesets instead.", getLocation());
|
---|
91 | } else {
|
---|
92 | expandFile(fileUtils, source, dest);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | if (filesets.size() > 0) {
|
---|
96 | for (int j = 0; j < filesets.size(); j++) {
|
---|
97 | FileSet fs = (FileSet) filesets.elementAt(j);
|
---|
98 | DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
---|
99 | File fromDir = fs.getDir(getProject());
|
---|
100 |
|
---|
101 | String[] files = ds.getIncludedFiles();
|
---|
102 | for (int i = 0; i < files.length; ++i) {
|
---|
103 | File file = new File(fromDir, files[i]);
|
---|
104 | expandFile(fileUtils, file, dest);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * This method is to be overridden by extending unarchival tasks.
|
---|
112 | */
|
---|
113 | protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
|
---|
114 | log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
|
---|
115 | ZipFile zf = null;
|
---|
116 | try {
|
---|
117 | zf = new ZipFile(srcF, encoding);
|
---|
118 | Enumeration e = zf.getEntries();
|
---|
119 | while (e.hasMoreElements()) {
|
---|
120 | ZipEntry ze = (ZipEntry) e.nextElement();
|
---|
121 | extractFile(fileUtils, srcF, dir, zf.getInputStream(ze),
|
---|
122 | ze.getName(), new Date(ze.getTime()),
|
---|
123 | ze.isDirectory());
|
---|
124 | }
|
---|
125 |
|
---|
126 | log("expand complete", Project.MSG_VERBOSE);
|
---|
127 | } catch (IOException ioe) {
|
---|
128 | throw new BuildException("Error while expanding " + srcF.getPath(),
|
---|
129 | ioe);
|
---|
130 | } finally {
|
---|
131 | if (zf != null) {
|
---|
132 | try {
|
---|
133 | zf.close();
|
---|
134 | } catch (IOException e) {
|
---|
135 | //ignore
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected void extractFile(FileUtils fileUtils, File srcF, File dir,
|
---|
142 | InputStream compressedInputStream,
|
---|
143 | String entryName,
|
---|
144 | Date entryDate, boolean isDirectory)
|
---|
145 | throws IOException {
|
---|
146 |
|
---|
147 | if (patternsets != null && patternsets.size() > 0) {
|
---|
148 | String name = entryName.replace('/', File.separatorChar)
|
---|
149 | .replace('\\', File.separatorChar);
|
---|
150 | boolean included = false;
|
---|
151 | for (int v = 0; v < patternsets.size(); v++) {
|
---|
152 | PatternSet p = (PatternSet) patternsets.elementAt(v);
|
---|
153 | String[] incls = p.getIncludePatterns(getProject());
|
---|
154 | if (incls == null || incls.length == 0) {
|
---|
155 | // no include pattern implicitly means includes="**"
|
---|
156 | incls = new String[] {"**"};
|
---|
157 | }
|
---|
158 |
|
---|
159 | for (int w = 0; w < incls.length; w++) {
|
---|
160 | String pattern = incls[w].replace('/', File.separatorChar)
|
---|
161 | .replace('\\', File.separatorChar);
|
---|
162 | if (pattern.endsWith(File.separator)) {
|
---|
163 | pattern += "**";
|
---|
164 | }
|
---|
165 |
|
---|
166 | included = SelectorUtils.matchPath(pattern, name);
|
---|
167 | if (included) {
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (!included) {
|
---|
173 | break;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | String[] excls = p.getExcludePatterns(getProject());
|
---|
178 | if (excls != null) {
|
---|
179 | for (int w = 0; w < excls.length; w++) {
|
---|
180 | String pattern = excls[w]
|
---|
181 | .replace('/', File.separatorChar)
|
---|
182 | .replace('\\', File.separatorChar);
|
---|
183 | if (pattern.endsWith(File.separator)) {
|
---|
184 | pattern += "**";
|
---|
185 | }
|
---|
186 | included = !(SelectorUtils.matchPath(pattern, name));
|
---|
187 | if (!included) {
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | if (!included) {
|
---|
194 | //Do not process this file
|
---|
195 | return;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | File f = fileUtils.resolveFile(dir, entryName);
|
---|
199 | try {
|
---|
200 | if (!overwrite && f.exists()
|
---|
201 | && f.lastModified() >= entryDate.getTime()) {
|
---|
202 | log("Skipping " + f + " as it is up-to-date",
|
---|
203 | Project.MSG_DEBUG);
|
---|
204 | return;
|
---|
205 | }
|
---|
206 |
|
---|
207 | log("expanding " + entryName + " to " + f,
|
---|
208 | Project.MSG_VERBOSE);
|
---|
209 | // create intermediary directories - sometimes zip don't add them
|
---|
210 | File dirF = fileUtils.getParentFile(f);
|
---|
211 | if (dirF != null) {
|
---|
212 | dirF.mkdirs();
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (isDirectory) {
|
---|
216 | f.mkdirs();
|
---|
217 | } else {
|
---|
218 | byte[] buffer = new byte[1024];
|
---|
219 | int length = 0;
|
---|
220 | FileOutputStream fos = null;
|
---|
221 | try {
|
---|
222 | fos = new FileOutputStream(f);
|
---|
223 |
|
---|
224 | while ((length =
|
---|
225 | compressedInputStream.read(buffer)) >= 0) {
|
---|
226 | fos.write(buffer, 0, length);
|
---|
227 | }
|
---|
228 |
|
---|
229 | fos.close();
|
---|
230 | fos = null;
|
---|
231 | } finally {
|
---|
232 | if (fos != null) {
|
---|
233 | try {
|
---|
234 | fos.close();
|
---|
235 | } catch (IOException e) {
|
---|
236 | // ignore
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | fileUtils.setFileLastModified(f, entryDate.getTime());
|
---|
243 | } catch (FileNotFoundException ex) {
|
---|
244 | log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
|
---|
245 | }
|
---|
246 |
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Set the destination directory. File will be unzipped into the
|
---|
251 | * destination directory.
|
---|
252 | *
|
---|
253 | * @param d Path to the directory.
|
---|
254 | */
|
---|
255 | public void setDest(File d) {
|
---|
256 | this.dest = d;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Set the path to zip-file.
|
---|
261 | *
|
---|
262 | * @param s Path to zip-file.
|
---|
263 | */
|
---|
264 | public void setSrc(File s) {
|
---|
265 | this.source = s;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Should we overwrite files in dest, even if they are newer than
|
---|
270 | * the corresponding entries in the archive?
|
---|
271 | */
|
---|
272 | public void setOverwrite(boolean b) {
|
---|
273 | overwrite = b;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Add a patternset
|
---|
278 | */
|
---|
279 | public void addPatternset(PatternSet set) {
|
---|
280 | patternsets.addElement(set);
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Add a fileset
|
---|
285 | */
|
---|
286 | public void addFileset(FileSet set) {
|
---|
287 | filesets.addElement(set);
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Sets the encoding to assume for file names and comments.
|
---|
292 | *
|
---|
293 | * <p>Set to <code>native-encoding</code> if you want your
|
---|
294 | * platform's native encoding, defaults to UTF8.</p>
|
---|
295 | *
|
---|
296 | * @since Ant 1.6
|
---|
297 | */
|
---|
298 | public void setEncoding(String encoding) {
|
---|
299 | if (NATIVE_ENCODING.equals(encoding)) {
|
---|
300 | encoding = null;
|
---|
301 | }
|
---|
302 | this.encoding = encoding;
|
---|
303 | }
|
---|
304 |
|
---|
305 | }
|
---|