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

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