You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Expand.java 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.BuildException;
  56. import org.apache.tools.ant.DirectoryScanner;
  57. import org.apache.tools.ant.Project;
  58. import org.apache.tools.ant.types.DestDir;
  59. import org.apache.tools.ant.types.FileSet;
  60. import org.apache.tools.ant.types.PatternSet;
  61. import org.apache.tools.ant.types.SrcFile;
  62. import org.apache.tools.ant.util.FileUtils;
  63. import java.io.File;
  64. import java.io.FileInputStream;
  65. import java.io.FileOutputStream;
  66. import java.io.FileNotFoundException;
  67. import java.io.InputStream;
  68. import java.io.IOException;
  69. import java.util.Date;
  70. import java.util.Vector;
  71. import java.util.zip.ZipInputStream;
  72. import java.util.zip.ZipEntry;
  73. /**
  74. * Unzip a file.
  75. *
  76. * @author costin@dnt.ro
  77. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  78. * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
  79. */
  80. public class Expand extends MatchingTask {
  81. private File dest; //req
  82. private File source; // req
  83. private boolean overwrite = true;
  84. private Vector patternsets = new Vector();
  85. private Vector filesets = new Vector();
  86. /**
  87. * Do the work.
  88. *
  89. * @exception BuildException Thrown in unrecoverable error.
  90. */
  91. public void execute() throws BuildException {
  92. if ("expand".equals(taskType)) {
  93. log("!! expand is deprecated. Use unzip instead. !!");
  94. }
  95. if (source == null && filesets.size() == 0) {
  96. throw new BuildException("src attribute and/or filesets must be specified");
  97. }
  98. if (dest == null) {
  99. throw new BuildException(
  100. "Dest attribute must be specified");
  101. }
  102. FileUtils fileUtils = FileUtils.newFileUtils();
  103. if (source != null) {
  104. expandFile(fileUtils, source, dest);
  105. }
  106. if (filesets.size() > 0) {
  107. for (int j=0; j < filesets.size(); j++) {
  108. FileSet fs = (FileSet) filesets.elementAt(j);
  109. DirectoryScanner ds = fs.getDirectoryScanner(project);
  110. File fromDir = fs.getDir(project);
  111. String[] files = ds.getIncludedFiles();
  112. for (int i = 0; i < files.length; ++i) {
  113. File file = new File(fromDir, files[i]);
  114. expandFile(fileUtils, file, dest);
  115. }
  116. }
  117. }
  118. }
  119. /*
  120. * This method is to be overridden by extending unarchival tasks.
  121. */
  122. protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
  123. ZipInputStream zis = null;
  124. try {
  125. // code from WarExpand
  126. zis = new ZipInputStream(new FileInputStream(srcF));
  127. ZipEntry ze = null;
  128. while ((ze = zis.getNextEntry()) != null) {
  129. extractFile(fileUtils, srcF, dir, zis,
  130. ze.getName(),
  131. new Date(ze.getTime()),
  132. ze.isDirectory());
  133. }
  134. log("expand complete", Project.MSG_VERBOSE );
  135. } catch (IOException ioe) {
  136. throw new BuildException("Error while expanding " + srcF.getPath(), ioe);
  137. } finally {
  138. if (zis != null) {
  139. try {
  140. zis.close();
  141. }
  142. catch (IOException e) {}
  143. }
  144. }
  145. }
  146. protected void extractFile(FileUtils fileUtils, File srcF, File dir,
  147. InputStream compressedInputStream,
  148. String entryName,
  149. Date entryDate, boolean isDirectory)
  150. throws IOException {
  151. if (patternsets != null && patternsets.size() > 0) {
  152. String name = entryName;
  153. boolean included = false;
  154. for (int v = 0; v < patternsets.size(); v++) {
  155. PatternSet p = (PatternSet) patternsets.elementAt(v);
  156. String[] incls = p.getIncludePatterns(project);
  157. if (incls != null) {
  158. for (int w = 0; w < incls.length; w++) {
  159. boolean isIncl = DirectoryScanner.match(incls[w], name);
  160. if (isIncl) {
  161. included = true;
  162. break;
  163. }
  164. }
  165. }
  166. String[] excls = p.getExcludePatterns(project);
  167. if (excls != null) {
  168. for (int w = 0; w < excls.length; w++) {
  169. boolean isExcl = DirectoryScanner.match(excls[w], name);
  170. if (isExcl) {
  171. included = false;
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. if (!included) {
  178. //Do not process this file
  179. return;
  180. }
  181. }
  182. File f = fileUtils.resolveFile(dir, entryName);
  183. try {
  184. if (!overwrite && f.exists()
  185. && f.lastModified() >= entryDate.getTime()) {
  186. log("Skipping " + f + " as it is up-to-date",
  187. Project.MSG_DEBUG);
  188. return;
  189. }
  190. log("expanding " + entryName + " to "+ f,
  191. Project.MSG_VERBOSE);
  192. // create intermediary directories - sometimes zip don't add them
  193. File dirF= fileUtils.getParentFile(f);
  194. dirF.mkdirs();
  195. if (isDirectory) {
  196. f.mkdirs();
  197. } else {
  198. byte[] buffer = new byte[1024];
  199. int length = 0;
  200. FileOutputStream fos = null;
  201. try {
  202. fos = new FileOutputStream(f);
  203. while ((length =
  204. compressedInputStream.read(buffer)) >= 0) {
  205. fos.write(buffer, 0, length);
  206. }
  207. fos.close();
  208. fos = null;
  209. } finally {
  210. if (fos != null) {
  211. try {
  212. fos.close();
  213. } catch (IOException e) {}
  214. }
  215. }
  216. }
  217. fileUtils.setFileLastModified(f, entryDate.getTime());
  218. } catch( FileNotFoundException ex ) {
  219. log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
  220. }
  221. }
  222. /**
  223. * Set the destination directory. File will be unzipped into the
  224. * destination directory.
  225. *
  226. * @param d Path to the directory.
  227. * @deprecated setDest(File) is deprecated and is replaced with
  228. * setDest(DestDir) to let Ant's core perform validation
  229. */
  230. public void setDest(File d) {
  231. log("DEPRECATED - The setDest(File) method has been deprecated."
  232. + " Use setDest(DestDir) instead.");
  233. DestDir dd = new DestDir();
  234. dd.setFile(d);
  235. setDest(dd);
  236. }
  237. /**
  238. * Set the destination directory. File will be unzipped into the
  239. * destination directory.
  240. *
  241. * @param d Path to the directory.
  242. */
  243. public void setDest(DestDir d) {
  244. this.dest=d.getFile();
  245. }
  246. /**
  247. * Set the path to zip-file.
  248. *
  249. * @param s Path to zip-file.
  250. * @deprecated setSrc(File) is deprecated and is replaced with
  251. * setSrc(SrcFile) to let Ant's core perform validation
  252. */
  253. public void setSrc(File s) {
  254. log("DEPRECATED - The setSrc(File) method has been deprecated."
  255. + " Use setSrc(SrcFile) instead.");
  256. SrcFile sf = new SrcFile();
  257. sf.setFile(s);
  258. setSrc(sf);
  259. }
  260. /**
  261. * Set the path to zip-file.
  262. *
  263. * @param s Path to zip-file.
  264. */
  265. public void setSrc(SrcFile s) {
  266. this.source = s.getFile();
  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. * Add a patternset
  277. */
  278. public void addPatternset(PatternSet set) {
  279. patternsets.addElement(set);
  280. }
  281. /**
  282. * Add a fileset
  283. */
  284. public void addFileset(FileSet set) {
  285. filesets.addElement(set);
  286. }
  287. }