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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 java.io.File;
  56. import java.io.FileInputStream;
  57. import java.io.FileNotFoundException;
  58. import java.io.FileOutputStream;
  59. import java.io.IOException;
  60. import java.io.InputStream;
  61. import java.util.Date;
  62. import java.util.Vector;
  63. import java.util.zip.ZipEntry;
  64. import java.util.zip.ZipInputStream;
  65. import org.apache.tools.ant.BuildException;
  66. import org.apache.tools.ant.DirectoryScanner;
  67. import org.apache.tools.ant.Project;
  68. import org.apache.tools.ant.Task;
  69. import org.apache.tools.ant.types.FileSet;
  70. import org.apache.tools.ant.types.PatternSet;
  71. import org.apache.tools.ant.util.FileUtils;
  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 Magesh Umasankar
  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. included = true;
  171. PatternSet p = (PatternSet) patternsets.elementAt(v);
  172. String[] incls = p.getIncludePatterns(getProject());
  173. if (incls != null) {
  174. for (int w = 0; w < incls.length; w++) {
  175. included = DirectoryScanner.match(incls[w], name);
  176. if (included) {
  177. break;
  178. }
  179. }
  180. }
  181. String[] excls = p.getExcludePatterns(getProject());
  182. if (excls != null) {
  183. for (int w = 0; w < excls.length; w++) {
  184. included = !(DirectoryScanner.match(excls[w], name));
  185. if (!included) {
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. if (!included) {
  192. //Do not process this file
  193. return;
  194. }
  195. }
  196. File f = fileUtils.resolveFile(dir, entryName);
  197. try {
  198. if (!overwrite && f.exists()
  199. && f.lastModified() >= entryDate.getTime()) {
  200. log("Skipping " + f + " as it is up-to-date",
  201. Project.MSG_DEBUG);
  202. return;
  203. }
  204. log("expanding " + entryName + " to " + f,
  205. Project.MSG_VERBOSE);
  206. // create intermediary directories - sometimes zip don't add them
  207. File dirF = fileUtils.getParentFile(f);
  208. if ( dirF != null ) {
  209. dirF.mkdirs();
  210. }
  211. if (isDirectory) {
  212. f.mkdirs();
  213. } else {
  214. byte[] buffer = new byte[1024];
  215. int length = 0;
  216. FileOutputStream fos = null;
  217. try {
  218. fos = new FileOutputStream(f);
  219. while ((length =
  220. compressedInputStream.read(buffer)) >= 0) {
  221. fos.write(buffer, 0, length);
  222. }
  223. fos.close();
  224. fos = null;
  225. } finally {
  226. if (fos != null) {
  227. try {
  228. fos.close();
  229. } catch (IOException e) {}
  230. }
  231. }
  232. }
  233. fileUtils.setFileLastModified(f, entryDate.getTime());
  234. } catch (FileNotFoundException ex) {
  235. log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
  236. }
  237. }
  238. /**
  239. * Set the destination directory. File will be unzipped into the
  240. * destination directory.
  241. *
  242. * @param d Path to the directory.
  243. */
  244. public void setDest(File d) {
  245. this.dest = d;
  246. }
  247. /**
  248. * Set the path to zip-file.
  249. *
  250. * @param s Path to zip-file.
  251. */
  252. public void setSrc(File s) {
  253. this.source = s;
  254. }
  255. /**
  256. * Should we overwrite files in dest, even if they are newer than
  257. * the corresponding entries in the archive?
  258. */
  259. public void setOverwrite(boolean b) {
  260. overwrite = b;
  261. }
  262. /**
  263. * Add a patternset
  264. */
  265. public void addPatternset(PatternSet set) {
  266. patternsets.addElement(set);
  267. }
  268. /**
  269. * Add a fileset
  270. */
  271. public void addFileset(FileSet set) {
  272. filesets.addElement(set);
  273. }
  274. }