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