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

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