[14982] | 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 | }
|
---|
[16675] | 206 |
|
---|
| 207 | System.out.println( "Expanding " + entryName );
|
---|
[14982] | 208 | log("expanding " + entryName + " to " + f,
|
---|
| 209 | Project.MSG_VERBOSE);
|
---|
| 210 | // create intermediary directories - sometimes zip don't add them
|
---|
| 211 | File dirF = fileUtils.getParentFile(f);
|
---|
| 212 | if (dirF != null) {
|
---|
| 213 | dirF.mkdirs();
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | if (isDirectory) {
|
---|
| 217 | f.mkdirs();
|
---|
| 218 | } else {
|
---|
| 219 | byte[] buffer = new byte[1024];
|
---|
| 220 | int length = 0;
|
---|
| 221 | FileOutputStream fos = null;
|
---|
| 222 | try {
|
---|
| 223 | fos = new FileOutputStream(f);
|
---|
| 224 |
|
---|
| 225 | while ((length =
|
---|
| 226 | compressedInputStream.read(buffer)) >= 0) {
|
---|
| 227 | fos.write(buffer, 0, length);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | fos.close();
|
---|
| 231 | fos = null;
|
---|
| 232 | } finally {
|
---|
| 233 | if (fos != null) {
|
---|
| 234 | try {
|
---|
| 235 | fos.close();
|
---|
| 236 | } catch (IOException e) {
|
---|
| 237 | // ignore
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | fileUtils.setFileLastModified(f, entryDate.getTime());
|
---|
| 244 | } catch (FileNotFoundException ex) {
|
---|
| 245 | log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /**
|
---|
| 251 | * Set the destination directory. File will be unzipped into the
|
---|
| 252 | * destination directory.
|
---|
| 253 | *
|
---|
| 254 | * @param d Path to the directory.
|
---|
| 255 | */
|
---|
| 256 | public void setDest(File d) {
|
---|
| 257 | this.dest = d;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | /**
|
---|
| 261 | * Set the path to zip-file.
|
---|
| 262 | *
|
---|
| 263 | * @param s Path to zip-file.
|
---|
| 264 | */
|
---|
| 265 | public void setSrc(File s) {
|
---|
| 266 | this.source = s;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | /**
|
---|
| 270 | * Should we overwrite files in dest, even if they are newer than
|
---|
| 271 | * the corresponding entries in the archive?
|
---|
| 272 | */
|
---|
| 273 | public void setOverwrite(boolean b) {
|
---|
| 274 | overwrite = b;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /**
|
---|
| 278 | * Add a patternset
|
---|
| 279 | */
|
---|
| 280 | public void addPatternset(PatternSet set) {
|
---|
| 281 | patternsets.addElement(set);
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | /**
|
---|
| 285 | * Add a fileset
|
---|
| 286 | */
|
---|
| 287 | public void addFileset(FileSet set) {
|
---|
| 288 | filesets.addElement(set);
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /**
|
---|
| 292 | * Sets the encoding to assume for file names and comments.
|
---|
| 293 | *
|
---|
| 294 | * <p>Set to <code>native-encoding</code> if you want your
|
---|
| 295 | * platform's native encoding, defaults to UTF8.</p>
|
---|
| 296 | *
|
---|
| 297 | * @since Ant 1.6
|
---|
| 298 | */
|
---|
| 299 | public void setEncoding(String encoding) {
|
---|
| 300 | if (NATIVE_ENCODING.equals(encoding)) {
|
---|
| 301 | encoding = null;
|
---|
| 302 | }
|
---|
| 303 | this.encoding = encoding;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | }
|
---|