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 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.FileSet;
  59. import org.apache.tools.ant.types.PatternSet;
  60. import org.apache.tools.ant.util.FileUtils;
  61. import java.io.File;
  62. import java.io.FileInputStream;
  63. import java.io.FileOutputStream;
  64. import java.io.FileNotFoundException;
  65. import java.io.InputStream;
  66. import java.io.IOException;
  67. import java.util.Date;
  68. import java.util.Vector;
  69. import java.util.zip.ZipInputStream;
  70. import java.util.zip.ZipEntry;
  71. /**
  72. * Unzip a file.
  73. *
  74. * @author costin@dnt.ro
  75. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  76. * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
  77. */
  78. public class Expand extends MatchingTask {
  79. private File dest; //req
  80. private File source; // req
  81. private boolean overwrite = true;
  82. private Vector patternsets = new Vector();
  83. private Vector filesets = new Vector();
  84. /**
  85. * Do the work.
  86. *
  87. * @exception BuildException Thrown in unrecoverable error.
  88. */
  89. public void execute() throws BuildException {
  90. if ("expand".equals(taskType)) {
  91. log("!! expand is deprecated. Use unzip instead. !!");
  92. }
  93. if (source == null && filesets.size() == 0) {
  94. throw new BuildException("src attribute and/or filesets must be specified");
  95. }
  96. if (dest == null) {
  97. throw new BuildException(
  98. "Dest attribute must be specified");
  99. }
  100. if (dest.exists() && !dest.isDirectory()) {
  101. throw new BuildException("Dest must be a directory.", location);
  102. }
  103. FileUtils fileUtils = FileUtils.newFileUtils();
  104. if (source != null) {
  105. if (source.isDirectory()) {
  106. throw new BuildException("Src must not be a directory." +
  107. " Use nested filesets instead.", location);
  108. } else {
  109. expandFile(fileUtils, source, dest);
  110. }
  111. }
  112. if (filesets.size() > 0) {
  113. for (int j=0; j < filesets.size(); j++) {
  114. FileSet fs = (FileSet) filesets.elementAt(j);
  115. DirectoryScanner ds = fs.getDirectoryScanner(project);
  116. File fromDir = fs.getDir(project);
  117. String[] files = ds.getIncludedFiles();
  118. for (int i = 0; i < files.length; ++i) {
  119. File file = new File(fromDir, files[i]);
  120. expandFile(fileUtils, file, dest);
  121. }
  122. }
  123. }
  124. }
  125. /*
  126. * This method is to be overridden by extending unarchival tasks.
  127. */
  128. protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
  129. ZipInputStream zis = null;
  130. try {
  131. // code from WarExpand
  132. zis = new ZipInputStream(new FileInputStream(srcF));
  133. ZipEntry ze = null;
  134. while ((ze = zis.getNextEntry()) != null) {
  135. extractFile(fileUtils, srcF, dir, zis,
  136. ze.getName(),
  137. new Date(ze.getTime()),
  138. ze.isDirectory());
  139. }
  140. log("expand complete", Project.MSG_VERBOSE );
  141. } catch (IOException ioe) {
  142. throw new BuildException("Error while expanding " + srcF.getPath(), ioe);
  143. } finally {
  144. if (zis != null) {
  145. try {
  146. zis.close();
  147. }
  148. catch (IOException e) {}
  149. }
  150. }
  151. }
  152. protected void extractFile(FileUtils fileUtils, File srcF, File dir,
  153. InputStream compressedInputStream,
  154. String entryName,
  155. Date entryDate, boolean isDirectory)
  156. throws IOException {
  157. if (patternsets != null && patternsets.size() > 0) {
  158. String name = entryName;
  159. boolean included = false;
  160. for (int v = 0; v < patternsets.size(); v++) {
  161. PatternSet p = (PatternSet) patternsets.elementAt(v);
  162. String[] incls = p.getIncludePatterns(project);
  163. if (incls != null) {
  164. for (int w = 0; w < incls.length; w++) {
  165. boolean isIncl = DirectoryScanner.match(incls[w], name);
  166. if (isIncl) {
  167. included = true;
  168. break;
  169. }
  170. }
  171. }
  172. String[] excls = p.getExcludePatterns(project);
  173. if (excls != null) {
  174. for (int w = 0; w < excls.length; w++) {
  175. boolean isExcl = DirectoryScanner.match(excls[w], name);
  176. if (isExcl) {
  177. included = false;
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. if (!included) {
  184. //Do not process this file
  185. return;
  186. }
  187. }
  188. log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
  189. File f = fileUtils.resolveFile(dir, entryName);
  190. try {
  191. if (!overwrite && f.exists()
  192. && f.lastModified() >= entryDate.getTime()) {
  193. log("Skipping " + f + " as it is up-to-date",
  194. Project.MSG_DEBUG);
  195. return;
  196. }
  197. log("expanding " + entryName + " to "+ f,
  198. Project.MSG_VERBOSE);
  199. // create intermediary directories - sometimes zip don't add them
  200. File dirF= fileUtils.getParentFile(f);
  201. dirF.mkdirs();
  202. if (isDirectory) {
  203. f.mkdirs();
  204. } else {
  205. byte[] buffer = new byte[1024];
  206. int length = 0;
  207. FileOutputStream fos = null;
  208. try {
  209. fos = new FileOutputStream(f);
  210. while ((length =
  211. compressedInputStream.read(buffer)) >= 0) {
  212. fos.write(buffer, 0, length);
  213. }
  214. fos.close();
  215. fos = null;
  216. } finally {
  217. if (fos != null) {
  218. try {
  219. fos.close();
  220. } catch (IOException e) {}
  221. }
  222. }
  223. }
  224. fileUtils.setFileLastModified(f, entryDate.getTime());
  225. } catch( FileNotFoundException ex ) {
  226. log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
  227. }
  228. }
  229. /**
  230. * Set the destination directory. File will be unzipped into the
  231. * destination directory.
  232. *
  233. * @param d Path to the directory.
  234. */
  235. public void setDest(File d) {
  236. this.dest=d;
  237. }
  238. /**
  239. * Set the path to zip-file.
  240. *
  241. * @param s Path to zip-file.
  242. */
  243. public void setSrc(File s) {
  244. this.source = s;
  245. }
  246. /**
  247. * Should we overwrite files in dest, even if they are newer than
  248. * the corresponding entries in the archive?
  249. */
  250. public void setOverwrite(boolean b) {
  251. overwrite = b;
  252. }
  253. /**
  254. * Add a patternset
  255. */
  256. public void addPatternset(PatternSet set) {
  257. patternsets.addElement(set);
  258. }
  259. /**
  260. * Add a fileset
  261. */
  262. public void addFileset(FileSet set) {
  263. filesets.addElement(set);
  264. }
  265. }