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.

Untar.java 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.BufferedInputStream;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.zip.GZIPInputStream;
  25. import org.apache.tools.ant.BuildException;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.types.EnumeratedAttribute;
  28. import org.apache.tools.ant.types.Resource;
  29. import org.apache.tools.ant.util.FileNameMapper;
  30. import org.apache.tools.ant.util.FileUtils;
  31. import org.apache.tools.bzip2.CBZip2InputStream;
  32. import org.apache.tools.tar.TarEntry;
  33. import org.apache.tools.tar.TarInputStream;
  34. /**
  35. * Untar a file.
  36. * <p>PatternSets are used to select files to extract
  37. * <I>from</I> the archive. If no patternset is used, all files are extracted.
  38. * </p>
  39. * <p>FileSet>s may be used to select archived files
  40. * to perform unarchival upon.
  41. * </p>
  42. * <p>File permissions will not be restored on extracted files.</p>
  43. * <p>The untar task recognizes the long pathname entries used by GNU tar.<p>
  44. *
  45. * @since Ant 1.1
  46. *
  47. * @ant.task category="packaging"
  48. */
  49. public class Untar extends Expand {
  50. /**
  51. * compression method
  52. */
  53. private UntarCompressionMethod compression = new UntarCompressionMethod();
  54. /**
  55. * Set decompression algorithm to use; default=none.
  56. *
  57. * Allowable values are
  58. * <ul>
  59. * <li>none - no compression
  60. * <li>gzip - Gzip compression
  61. * <li>bzip2 - Bzip2 compression
  62. * </ul>
  63. *
  64. * @param method compression method
  65. */
  66. public void setCompression(UntarCompressionMethod method) {
  67. compression = method;
  68. }
  69. /**
  70. * No encoding support in Untar.
  71. * @param encoding not used
  72. * @throws BuildException always
  73. * @since Ant 1.6
  74. */
  75. public void setEncoding(String encoding) {
  76. throw new BuildException("The " + getTaskName()
  77. + " task doesn't support the encoding"
  78. + " attribute", getLocation());
  79. }
  80. /**
  81. * @see Expand#expandFile(FileUtils, File, File)
  82. */
  83. /** {@inheritDoc} */
  84. protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
  85. FileInputStream fis = null;
  86. if (!srcF.exists()) {
  87. throw new BuildException("Unable to untar "
  88. + srcF
  89. + " as the file does not exist",
  90. getLocation());
  91. }
  92. try {
  93. fis = new FileInputStream(srcF);
  94. expandStream(srcF.getPath(), fis, dir);
  95. } catch (IOException ioe) {
  96. throw new BuildException("Error while expanding " + srcF.getPath()
  97. + "\n" + ioe.toString(),
  98. ioe, getLocation());
  99. } finally {
  100. FileUtils.close(fis);
  101. }
  102. }
  103. /**
  104. * This method is to be overridden by extending unarchival tasks.
  105. *
  106. * @param srcR the source resource
  107. * @param dir the destination directory
  108. * @since Ant 1.7
  109. */
  110. protected void expandResource(Resource srcR, File dir) {
  111. if (!srcR.isExists()) {
  112. throw new BuildException("Unable to untar "
  113. + srcR.getName()
  114. + " as the it does not exist",
  115. getLocation());
  116. }
  117. InputStream i = null;
  118. try {
  119. i = srcR.getInputStream();
  120. expandStream(srcR.getName(), i, dir);
  121. } catch (IOException ioe) {
  122. throw new BuildException("Error while expanding " + srcR.getName(),
  123. ioe, getLocation());
  124. } finally {
  125. FileUtils.close(i);
  126. }
  127. }
  128. /**
  129. * @since Ant 1.7
  130. */
  131. private void expandStream(String name, InputStream stream, File dir)
  132. throws IOException {
  133. TarInputStream tis = null;
  134. try {
  135. tis =
  136. new TarInputStream(compression.decompress(name,
  137. new BufferedInputStream(stream)));
  138. log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
  139. TarEntry te = null;
  140. boolean empty = true;
  141. FileNameMapper mapper = getMapper();
  142. while ((te = tis.getNextEntry()) != null) {
  143. empty = false;
  144. extractFile(FileUtils.getFileUtils(), null, dir, tis,
  145. te.getName(), te.getModTime(),
  146. te.isDirectory(), mapper);
  147. }
  148. if (empty && getFailOnEmptyArchive()) {
  149. throw new BuildException("archive is empty");
  150. }
  151. log("expand complete", Project.MSG_VERBOSE);
  152. } finally {
  153. FileUtils.close(tis);
  154. }
  155. }
  156. /**
  157. * Valid Modes for Compression attribute to Untar Task
  158. *
  159. */
  160. public static final class UntarCompressionMethod
  161. extends EnumeratedAttribute {
  162. // permissible values for compression attribute
  163. /**
  164. * No compression
  165. */
  166. private static final String NONE = "none";
  167. /**
  168. * GZIP compression
  169. */
  170. private static final String GZIP = "gzip";
  171. /**
  172. * BZIP2 compression
  173. */
  174. private static final String BZIP2 = "bzip2";
  175. /**
  176. * Constructor
  177. */
  178. public UntarCompressionMethod() {
  179. super();
  180. setValue(NONE);
  181. }
  182. /**
  183. * Get valid enumeration values
  184. *
  185. * @return valid values
  186. */
  187. public String[] getValues() {
  188. return new String[] {NONE, GZIP, BZIP2};
  189. }
  190. /**
  191. * This method wraps the input stream with the
  192. * corresponding decompression method
  193. *
  194. * @param name provides location information for BuildException
  195. * @param istream input stream
  196. * @return input stream with on-the-fly decompression
  197. * @exception IOException thrown by GZIPInputStream constructor
  198. * @exception BuildException thrown if bzip stream does not
  199. * start with expected magic values
  200. */
  201. public InputStream decompress(final String name,
  202. final InputStream istream)
  203. throws IOException, BuildException {
  204. final String v = getValue();
  205. if (GZIP.equals(v)) {
  206. return new GZIPInputStream(istream);
  207. } else {
  208. if (BZIP2.equals(v)) {
  209. final char[] magic = new char[] {'B', 'Z'};
  210. for (int i = 0; i < magic.length; i++) {
  211. if (istream.read() != magic[i]) {
  212. throw new BuildException(
  213. "Invalid bz2 file." + name);
  214. }
  215. }
  216. return new CBZip2InputStream(istream);
  217. }
  218. }
  219. return istream;
  220. }
  221. }
  222. }