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

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