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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.Date;
  25. import java.util.Enumeration;
  26. import java.util.HashSet;
  27. import java.util.Iterator;
  28. import java.util.Set;
  29. import java.util.Vector;
  30. import org.apache.tools.ant.BuildException;
  31. import org.apache.tools.ant.Project;
  32. import org.apache.tools.ant.Task;
  33. import org.apache.tools.ant.types.FileSet;
  34. import org.apache.tools.ant.types.Mapper;
  35. import org.apache.tools.ant.types.PatternSet;
  36. import org.apache.tools.ant.types.Resource;
  37. import org.apache.tools.ant.types.ResourceCollection;
  38. import org.apache.tools.ant.types.resources.FileProvider;
  39. import org.apache.tools.ant.types.resources.Union;
  40. import org.apache.tools.ant.types.selectors.SelectorUtils;
  41. import org.apache.tools.ant.util.FileNameMapper;
  42. import org.apache.tools.ant.util.FileUtils;
  43. import org.apache.tools.ant.util.IdentityMapper;
  44. import org.apache.tools.zip.ZipEntry;
  45. import org.apache.tools.zip.ZipFile;
  46. /**
  47. * Unzip a file.
  48. *
  49. * @since Ant 1.1
  50. *
  51. * @ant.task category="packaging"
  52. * name="unzip"
  53. * name="unjar"
  54. * name="unwar"
  55. */
  56. public class Expand extends Task {
  57. private static final int BUFFER_SIZE = 1024;
  58. private File dest; //req
  59. private File source; // req
  60. private boolean overwrite = true;
  61. private Mapper mapperElement = null;
  62. private Vector patternsets = new Vector();
  63. private Union resources = new Union();
  64. private boolean resourcesSpecified = false;
  65. private boolean failOnEmptyArchive = false;
  66. private boolean stripAbsolutePathSpec = false;
  67. private boolean scanForUnicodeExtraFields = true;
  68. public static final String NATIVE_ENCODING = "native-encoding";
  69. private String encoding = "UTF8";
  70. /** Error message when more that one mapper is defined */
  71. public static final String ERROR_MULTIPLE_MAPPERS = "Cannot define more than one mapper";
  72. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  73. /**
  74. * Whether try ing to expand an empty archive would be an error.
  75. *
  76. * @since Ant 1.8.0
  77. */
  78. public void setFailOnEmptyArchive(boolean b) {
  79. failOnEmptyArchive = b;
  80. }
  81. /**
  82. * Whether try ing to expand an empty archive would be an error.
  83. *
  84. * @since Ant 1.8.0
  85. */
  86. public boolean getFailOnEmptyArchive() {
  87. return failOnEmptyArchive;
  88. }
  89. /**
  90. * Do the work.
  91. *
  92. * @exception BuildException Thrown in unrecoverable error.
  93. */
  94. public void execute() throws BuildException {
  95. if ("expand".equals(getTaskType())) {
  96. log("!! expand is deprecated. Use unzip instead. !!");
  97. }
  98. if (source == null && !resourcesSpecified) {
  99. throw new BuildException("src attribute and/or resources must be "
  100. + "specified");
  101. }
  102. if (dest == null) {
  103. throw new BuildException(
  104. "Dest attribute must be specified");
  105. }
  106. if (dest.exists() && !dest.isDirectory()) {
  107. throw new BuildException("Dest must be a directory.", getLocation());
  108. }
  109. if (source != null) {
  110. if (source.isDirectory()) {
  111. throw new BuildException("Src must not be a directory."
  112. + " Use nested filesets instead.", getLocation());
  113. } else if (!source.exists()) {
  114. throw new BuildException("src '" + source + "' doesn't exist.");
  115. } else if (!source.canRead()) {
  116. throw new BuildException("src '" + source + "' cannot be read.");
  117. } else {
  118. expandFile(FILE_UTILS, source, dest);
  119. }
  120. }
  121. for (Resource r : resources) {
  122. if (!r.isExists()) {
  123. log("Skipping '" + r.getName() + "' because it doesn't exist.");
  124. continue;
  125. }
  126. FileProvider fp = r.as(FileProvider.class);
  127. if (fp != null) {
  128. expandFile(FILE_UTILS, fp.getFile(), dest);
  129. } else {
  130. expandResource(r, dest);
  131. }
  132. }
  133. }
  134. /**
  135. * This method is to be overridden by extending unarchival tasks.
  136. *
  137. * @param fileUtils the fileUtils
  138. * @param srcF the source file
  139. * @param dir the destination directory
  140. */
  141. protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
  142. log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
  143. ZipFile zf = null;
  144. FileNameMapper mapper = getMapper();
  145. if (!srcF.exists()) {
  146. throw new BuildException("Unable to expand "
  147. + srcF
  148. + " as the file does not exist",
  149. getLocation());
  150. }
  151. try {
  152. zf = new ZipFile(srcF, encoding, scanForUnicodeExtraFields);
  153. boolean empty = true;
  154. Enumeration e = zf.getEntries();
  155. while (e.hasMoreElements()) {
  156. empty = false;
  157. ZipEntry ze = (ZipEntry) e.nextElement();
  158. InputStream is = null;
  159. log("extracting " + ze.getName(), Project.MSG_DEBUG);
  160. try {
  161. extractFile(fileUtils, srcF, dir,
  162. is = zf.getInputStream(ze),
  163. ze.getName(), new Date(ze.getTime()),
  164. ze.isDirectory(), mapper);
  165. } finally {
  166. FileUtils.close(is);
  167. }
  168. }
  169. if (empty && getFailOnEmptyArchive()) {
  170. throw new BuildException("archive '" + srcF + "' is empty");
  171. }
  172. log("expand complete", Project.MSG_VERBOSE);
  173. } catch (IOException ioe) {
  174. throw new BuildException(
  175. "Error while expanding " + srcF.getPath()
  176. + "\n" + ioe.toString(),
  177. ioe);
  178. } finally {
  179. ZipFile.closeQuietly(zf);
  180. }
  181. }
  182. /**
  183. * This method is to be overridden by extending unarchival tasks.
  184. *
  185. * @param srcR the source resource
  186. * @param dir the destination directory
  187. */
  188. protected void expandResource(Resource srcR, File dir) {
  189. throw new BuildException("only filesystem based resources are"
  190. + " supported by this task.");
  191. }
  192. /**
  193. * get a mapper for a file
  194. * @return a filenamemapper for a file
  195. */
  196. protected FileNameMapper getMapper() {
  197. FileNameMapper mapper = null;
  198. if (mapperElement != null) {
  199. mapper = mapperElement.getImplementation();
  200. } else {
  201. mapper = new IdentityMapper();
  202. }
  203. return mapper;
  204. }
  205. // CheckStyle:ParameterNumberCheck OFF - bc
  206. /**
  207. * extract a file to a directory
  208. * @param fileUtils a fileUtils object
  209. * @param srcF the source file
  210. * @param dir the destination directory
  211. * @param compressedInputStream the input stream
  212. * @param entryName the name of the entry
  213. * @param entryDate the date of the entry
  214. * @param isDirectory if this is true the entry is a directory
  215. * @param mapper the filename mapper to use
  216. * @throws IOException on error
  217. */
  218. protected void extractFile(FileUtils fileUtils, File srcF, File dir,
  219. InputStream compressedInputStream,
  220. String entryName, Date entryDate,
  221. boolean isDirectory, FileNameMapper mapper)
  222. throws IOException {
  223. if (stripAbsolutePathSpec && entryName.length() > 0
  224. && (entryName.charAt(0) == File.separatorChar
  225. || entryName.charAt(0) == '/'
  226. || entryName.charAt(0) == '\\')) {
  227. log("stripped absolute path spec from " + entryName,
  228. Project.MSG_VERBOSE);
  229. entryName = entryName.substring(1);
  230. }
  231. if (patternsets != null && patternsets.size() > 0) {
  232. String name = entryName.replace('/', File.separatorChar)
  233. .replace('\\', File.separatorChar);
  234. boolean included = false;
  235. Set includePatterns = new HashSet();
  236. Set excludePatterns = new HashSet();
  237. final int size = patternsets.size();
  238. for (int v = 0; v < size; v++) {
  239. PatternSet p = (PatternSet) patternsets.elementAt(v);
  240. String[] incls = p.getIncludePatterns(getProject());
  241. if (incls == null || incls.length == 0) {
  242. // no include pattern implicitly means includes="**"
  243. incls = new String[] {"**"};
  244. }
  245. for (int w = 0; w < incls.length; w++) {
  246. String pattern = incls[w].replace('/', File.separatorChar)
  247. .replace('\\', File.separatorChar);
  248. if (pattern.endsWith(File.separator)) {
  249. pattern += "**";
  250. }
  251. includePatterns.add(pattern);
  252. }
  253. String[] excls = p.getExcludePatterns(getProject());
  254. if (excls != null) {
  255. for (int w = 0; w < excls.length; w++) {
  256. String pattern = excls[w]
  257. .replace('/', File.separatorChar)
  258. .replace('\\', File.separatorChar);
  259. if (pattern.endsWith(File.separator)) {
  260. pattern += "**";
  261. }
  262. excludePatterns.add(pattern);
  263. }
  264. }
  265. }
  266. for (Iterator iter = includePatterns.iterator();
  267. !included && iter.hasNext();) {
  268. String pattern = (String) iter.next();
  269. included = SelectorUtils.matchPath(pattern, name);
  270. }
  271. for (Iterator iter = excludePatterns.iterator();
  272. included && iter.hasNext();) {
  273. String pattern = (String) iter.next();
  274. included = !SelectorUtils.matchPath(pattern, name);
  275. }
  276. if (!included) {
  277. //Do not process this file
  278. log("skipping " + entryName
  279. + " as it is excluded or not included.",
  280. Project.MSG_VERBOSE);
  281. return;
  282. }
  283. }
  284. String[] mappedNames = mapper.mapFileName(entryName);
  285. if (mappedNames == null || mappedNames.length == 0) {
  286. mappedNames = new String[] {entryName};
  287. }
  288. File f = fileUtils.resolveFile(dir, mappedNames[0]);
  289. try {
  290. if (!overwrite && f.exists()
  291. && f.lastModified() >= entryDate.getTime()) {
  292. log("Skipping " + f + " as it is up-to-date",
  293. Project.MSG_DEBUG);
  294. return;
  295. }
  296. log("expanding " + entryName + " to " + f,
  297. Project.MSG_VERBOSE);
  298. // create intermediary directories - sometimes zip don't add them
  299. File dirF = f.getParentFile();
  300. if (dirF != null) {
  301. dirF.mkdirs();
  302. }
  303. if (isDirectory) {
  304. f.mkdirs();
  305. } else {
  306. byte[] buffer = new byte[BUFFER_SIZE];
  307. int length = 0;
  308. FileOutputStream fos = null;
  309. try {
  310. fos = new FileOutputStream(f);
  311. while ((length =
  312. compressedInputStream.read(buffer)) >= 0) {
  313. fos.write(buffer, 0, length);
  314. }
  315. fos.close();
  316. fos = null;
  317. } finally {
  318. FileUtils.close(fos);
  319. }
  320. }
  321. fileUtils.setFileLastModified(f, entryDate.getTime());
  322. } catch (FileNotFoundException ex) {
  323. log("Unable to expand to file " + f.getPath(),
  324. ex,
  325. Project.MSG_WARN);
  326. }
  327. }
  328. // CheckStyle:ParameterNumberCheck ON
  329. /**
  330. * Set the destination directory. File will be unzipped into the
  331. * destination directory.
  332. *
  333. * @param d Path to the directory.
  334. */
  335. public void setDest(File d) {
  336. this.dest = d;
  337. }
  338. /**
  339. * Set the path to zip-file.
  340. *
  341. * @param s Path to zip-file.
  342. */
  343. public void setSrc(File s) {
  344. this.source = s;
  345. }
  346. /**
  347. * Should we overwrite files in dest, even if they are newer than
  348. * the corresponding entries in the archive?
  349. * @param b a <code>boolean</code> value
  350. */
  351. public void setOverwrite(boolean b) {
  352. overwrite = b;
  353. }
  354. /**
  355. * Add a patternset.
  356. * @param set a pattern set
  357. */
  358. public void addPatternset(PatternSet set) {
  359. patternsets.addElement(set);
  360. }
  361. /**
  362. * Add a fileset
  363. * @param set a file set
  364. */
  365. public void addFileset(FileSet set) {
  366. add(set);
  367. }
  368. /**
  369. * Add a resource collection.
  370. * @param rc a resource collection.
  371. * @since Ant 1.7
  372. */
  373. public void add(ResourceCollection rc) {
  374. resourcesSpecified = true;
  375. resources.add(rc);
  376. }
  377. /**
  378. * Defines the mapper to map source entries to destination files.
  379. * @return a mapper to be configured
  380. * @exception BuildException if more than one mapper is defined
  381. * @since Ant1.7
  382. */
  383. public Mapper createMapper() throws BuildException {
  384. if (mapperElement != null) {
  385. throw new BuildException(ERROR_MULTIPLE_MAPPERS,
  386. getLocation());
  387. }
  388. mapperElement = new Mapper(getProject());
  389. return mapperElement;
  390. }
  391. /**
  392. * A nested filenamemapper
  393. * @param fileNameMapper the mapper to add
  394. * @since Ant 1.6.3
  395. */
  396. public void add(FileNameMapper fileNameMapper) {
  397. createMapper().add(fileNameMapper);
  398. }
  399. /**
  400. * Sets the encoding to assume for file names and comments.
  401. *
  402. * <p>Set to <code>native-encoding</code> if you want your
  403. * platform's native encoding, defaults to UTF8.</p>
  404. * @param encoding the name of the character encoding
  405. * @since Ant 1.6
  406. */
  407. public void setEncoding(String encoding) {
  408. internalSetEncoding(encoding);
  409. }
  410. /**
  411. * Supports grand-children that want to support the attribute
  412. * where the child-class doesn't (i.e. Unzip in the compress
  413. * Antlib).
  414. *
  415. * @since Ant 1.8.0
  416. */
  417. protected void internalSetEncoding(String encoding) {
  418. if (NATIVE_ENCODING.equals(encoding)) {
  419. encoding = null;
  420. }
  421. this.encoding = encoding;
  422. }
  423. /**
  424. * @since Ant 1.8.0
  425. */
  426. public String getEncoding() {
  427. return encoding;
  428. }
  429. /**
  430. * Whether leading path separators should be stripped.
  431. *
  432. * @since Ant 1.8.0
  433. */
  434. public void setStripAbsolutePathSpec(boolean b) {
  435. stripAbsolutePathSpec = b;
  436. }
  437. /**
  438. * Whether unicode extra fields will be used if present.
  439. *
  440. * @since Ant 1.8.0
  441. */
  442. public void setScanForUnicodeExtraFields(boolean b) {
  443. internalSetScanForUnicodeExtraFields(b);
  444. }
  445. /**
  446. * Supports grand-children that want to support the attribute
  447. * where the child-class doesn't (i.e. Unzip in the compress
  448. * Antlib).
  449. *
  450. * @since Ant 1.8.0
  451. */
  452. protected void internalSetScanForUnicodeExtraFields(boolean b) {
  453. scanForUnicodeExtraFields = b;
  454. }
  455. /**
  456. * @since Ant 1.8.0
  457. */
  458. public boolean getScanForUnicodeExtraFields() {
  459. return scanForUnicodeExtraFields;
  460. }
  461. }