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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 2000-2005 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. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.Date;
  24. import java.util.Enumeration;
  25. import java.util.Iterator;
  26. import java.util.Vector;
  27. import org.apache.tools.ant.BuildException;
  28. import org.apache.tools.ant.DirectoryScanner;
  29. import org.apache.tools.ant.Project;
  30. import org.apache.tools.ant.Task;
  31. import org.apache.tools.ant.types.FileSet;
  32. import org.apache.tools.ant.types.Mapper;
  33. import org.apache.tools.ant.types.PatternSet;
  34. import org.apache.tools.ant.types.Resource;
  35. import org.apache.tools.ant.types.ResourceCollection;
  36. import org.apache.tools.ant.types.resources.FileResource;
  37. import org.apache.tools.ant.types.resources.Union;
  38. import org.apache.tools.ant.types.selectors.SelectorUtils;
  39. import org.apache.tools.ant.util.FileNameMapper;
  40. import org.apache.tools.ant.util.FileUtils;
  41. import org.apache.tools.ant.util.IdentityMapper;
  42. import org.apache.tools.zip.ZipEntry;
  43. import org.apache.tools.zip.ZipFile;
  44. /**
  45. * Unzip a file.
  46. *
  47. * @since Ant 1.1
  48. *
  49. * @ant.task category="packaging"
  50. * name="unzip"
  51. * name="unjar"
  52. * name="unwar"
  53. */
  54. public class Expand extends Task {
  55. private File dest; //req
  56. private File source; // req
  57. private boolean overwrite = true;
  58. private Mapper mapperElement = null;
  59. private Vector patternsets = new Vector();
  60. private Union resources = new Union();
  61. private static final String NATIVE_ENCODING = "native-encoding";
  62. private String encoding = "UTF8";
  63. /** Error message when more that one mapper is defined */
  64. public static final String ERROR_MULTIPLE_MAPPERS = "Cannot define more than one mapper";
  65. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  66. /**
  67. * Do the work.
  68. *
  69. * @exception BuildException Thrown in unrecoverable error.
  70. */
  71. public void execute() throws BuildException {
  72. if ("expand".equals(getTaskType())) {
  73. log("!! expand is deprecated. Use unzip instead. !!");
  74. }
  75. if (source == null && resources.size() == 0) {
  76. throw new BuildException("src attribute and/or resources must be "
  77. + "specified");
  78. }
  79. if (dest == null) {
  80. throw new BuildException(
  81. "Dest attribute must be specified");
  82. }
  83. if (dest.exists() && !dest.isDirectory()) {
  84. throw new BuildException("Dest must be a directory.", getLocation());
  85. }
  86. if (source != null) {
  87. if (source.isDirectory()) {
  88. throw new BuildException("Src must not be a directory."
  89. + " Use nested filesets instead.", getLocation());
  90. } else {
  91. expandFile(FILE_UTILS, source, dest);
  92. }
  93. }
  94. Iterator iter = resources.iterator();
  95. while (iter.hasNext()) {
  96. Resource r = (Resource) iter.next();
  97. if (!r.isExists()) {
  98. continue;
  99. }
  100. if (r instanceof FileResource) {
  101. expandFile(FILE_UTILS, ((FileResource) r).getFile(), dest);
  102. } else {
  103. expandResource(r, dest);
  104. }
  105. }
  106. }
  107. /**
  108. * This method is to be overridden by extending unarchival tasks.
  109. *
  110. * @param fileUtils the fileUtils
  111. * @param srcF the source file
  112. * @param dir the destination directory
  113. */
  114. protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
  115. log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
  116. ZipFile zf = null;
  117. FileNameMapper mapper = getMapper();
  118. try {
  119. zf = new ZipFile(srcF, encoding);
  120. Enumeration e = zf.getEntries();
  121. while (e.hasMoreElements()) {
  122. ZipEntry ze = (ZipEntry) e.nextElement();
  123. extractFile(fileUtils, srcF, dir, zf.getInputStream(ze),
  124. ze.getName(), new Date(ze.getTime()),
  125. ze.isDirectory(), mapper);
  126. }
  127. log("expand complete", Project.MSG_VERBOSE);
  128. } catch (IOException ioe) {
  129. throw new BuildException("Error while expanding " + srcF.getPath(),
  130. ioe);
  131. } finally {
  132. ZipFile.closeQuietly(zf);
  133. }
  134. }
  135. /**
  136. * This method is to be overridden by extending unarchival tasks.
  137. *
  138. * @param r the source resource
  139. * @param dir the destination directory
  140. */
  141. protected void expandResource(Resource srcR, File dir) {
  142. throw new BuildException("only filesystem based resources are"
  143. + " supported by this task.");
  144. }
  145. /**
  146. * get a mapper for a file
  147. * @return a filenamemapper for a file
  148. */
  149. protected FileNameMapper getMapper() {
  150. FileNameMapper mapper = null;
  151. if (mapperElement != null) {
  152. mapper = mapperElement.getImplementation();
  153. } else {
  154. mapper = new IdentityMapper();
  155. }
  156. return mapper;
  157. }
  158. /**
  159. * extract a file to a directory
  160. * @param fileUtils a fileUtils object
  161. * @param srcF the source file
  162. * @param dir the destination directory
  163. * @param compressedInputStream the input stream
  164. * @param entryName the name of the entry
  165. * @param entryDate the date of the entry
  166. * @param isDirectory if this is true the entry is a directory
  167. * @param mapper the filename mapper to use
  168. * @throws IOException on error
  169. */
  170. protected void extractFile(FileUtils fileUtils, File srcF, File dir,
  171. InputStream compressedInputStream,
  172. String entryName, Date entryDate,
  173. boolean isDirectory, FileNameMapper mapper)
  174. throws IOException {
  175. if (patternsets != null && patternsets.size() > 0) {
  176. String name = entryName.replace('/', File.separatorChar)
  177. .replace('\\', File.separatorChar);
  178. boolean included = false;
  179. for (int v = 0, size = patternsets.size(); v < size; v++) {
  180. PatternSet p = (PatternSet) patternsets.elementAt(v);
  181. String[] incls = p.getIncludePatterns(getProject());
  182. if (incls == null || incls.length == 0) {
  183. // no include pattern implicitly means includes="**"
  184. incls = new String[] {"**"};
  185. }
  186. for (int w = 0; w < incls.length; w++) {
  187. String pattern = incls[w].replace('/', File.separatorChar)
  188. .replace('\\', File.separatorChar);
  189. if (pattern.endsWith(File.separator)) {
  190. pattern += "**";
  191. }
  192. included = SelectorUtils.matchPath(pattern, name);
  193. if (included) {
  194. break;
  195. }
  196. }
  197. if (!included) {
  198. break;
  199. }
  200. String[] excls = p.getExcludePatterns(getProject());
  201. if (excls != null) {
  202. for (int w = 0; w < excls.length; w++) {
  203. String pattern = excls[w]
  204. .replace('/', File.separatorChar)
  205. .replace('\\', File.separatorChar);
  206. if (pattern.endsWith(File.separator)) {
  207. pattern += "**";
  208. }
  209. included = !(SelectorUtils.matchPath(pattern, name));
  210. if (!included) {
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. if (!included) {
  217. //Do not process this file
  218. return;
  219. }
  220. }
  221. String[] mappedNames = mapper.mapFileName(entryName);
  222. if (mappedNames == null || mappedNames.length == 0) {
  223. mappedNames = new String[] {entryName};
  224. }
  225. File f = fileUtils.resolveFile(dir, mappedNames[0]);
  226. try {
  227. if (!overwrite && f.exists()
  228. && f.lastModified() >= entryDate.getTime()) {
  229. log("Skipping " + f + " as it is up-to-date",
  230. Project.MSG_DEBUG);
  231. return;
  232. }
  233. log("expanding " + entryName + " to " + f,
  234. Project.MSG_VERBOSE);
  235. // create intermediary directories - sometimes zip don't add them
  236. File dirF = f.getParentFile();
  237. if (dirF != null) {
  238. dirF.mkdirs();
  239. }
  240. if (isDirectory) {
  241. f.mkdirs();
  242. } else {
  243. byte[] buffer = new byte[1024];
  244. int length = 0;
  245. FileOutputStream fos = null;
  246. try {
  247. fos = new FileOutputStream(f);
  248. while ((length =
  249. compressedInputStream.read(buffer)) >= 0) {
  250. fos.write(buffer, 0, length);
  251. }
  252. fos.close();
  253. fos = null;
  254. } finally {
  255. if (fos != null) {
  256. try {
  257. fos.close();
  258. } catch (IOException e) {
  259. // ignore
  260. }
  261. }
  262. }
  263. }
  264. fileUtils.setFileLastModified(f, entryDate.getTime());
  265. } catch (FileNotFoundException ex) {
  266. log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
  267. }
  268. }
  269. /**
  270. * Set the destination directory. File will be unzipped into the
  271. * destination directory.
  272. *
  273. * @param d Path to the directory.
  274. */
  275. public void setDest(File d) {
  276. this.dest = d;
  277. }
  278. /**
  279. * Set the path to zip-file.
  280. *
  281. * @param s Path to zip-file.
  282. */
  283. public void setSrc(File s) {
  284. this.source = s;
  285. }
  286. /**
  287. * Should we overwrite files in dest, even if they are newer than
  288. * the corresponding entries in the archive?
  289. * @param b a <code>boolean</code> value
  290. */
  291. public void setOverwrite(boolean b) {
  292. overwrite = b;
  293. }
  294. /**
  295. * Add a patternset.
  296. * @param set a pattern set
  297. */
  298. public void addPatternset(PatternSet set) {
  299. patternsets.addElement(set);
  300. }
  301. /**
  302. * Add a fileset
  303. * @param set a file set
  304. */
  305. public void addFileset(FileSet set) {
  306. add(set);
  307. }
  308. /**
  309. * Add a resource collection.
  310. * @param rc a resource collection.
  311. * @since Ant 1.7
  312. */
  313. public void add(ResourceCollection rc) {
  314. resources.add(rc);
  315. }
  316. /**
  317. * Defines the mapper to map source entries to destination files.
  318. * @return a mapper to be configured
  319. * @exception BuildException if more than one mapper is defined
  320. * @since Ant1.7
  321. */
  322. public Mapper createMapper() throws BuildException {
  323. if (mapperElement != null) {
  324. throw new BuildException(ERROR_MULTIPLE_MAPPERS,
  325. getLocation());
  326. }
  327. mapperElement = new Mapper(getProject());
  328. return mapperElement;
  329. }
  330. /**
  331. * A nested filenamemapper
  332. * @param fileNameMapper the mapper to add
  333. * @since Ant 1.6.3
  334. */
  335. public void add(FileNameMapper fileNameMapper) {
  336. createMapper().add(fileNameMapper);
  337. }
  338. /**
  339. * Sets the encoding to assume for file names and comments.
  340. *
  341. * <p>Set to <code>native-encoding</code> if you want your
  342. * platform's native encoding, defaults to UTF8.</p>
  343. * @param encoding the name of the character encoding
  344. * @since Ant 1.6
  345. */
  346. public void setEncoding(String encoding) {
  347. if (NATIVE_ENCODING.equals(encoding)) {
  348. encoding = null;
  349. }
  350. this.encoding = encoding;
  351. }
  352. }