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

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