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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.util.FileNameMapper;
  28. import org.apache.tools.ant.util.FileUtils;
  29. import org.apache.tools.bzip2.CBZip2InputStream;
  30. import org.apache.tools.tar.TarEntry;
  31. import org.apache.tools.tar.TarInputStream;
  32. /**
  33. * Untar a file.
  34. * <p>For JDK 1.1 &quot;last modified time&quot; field is set to current time instead of being
  35. * carried from the archive file.</p>
  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. protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
  84. TarInputStream tis = null;
  85. try {
  86. log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
  87. tis = new TarInputStream(
  88. compression.decompress(srcF,
  89. new BufferedInputStream(
  90. new FileInputStream(srcF))));
  91. TarEntry te = null;
  92. FileNameMapper mapper = getMapper();
  93. while ((te = tis.getNextEntry()) != null) {
  94. extractFile(fileUtils, srcF, dir, tis,
  95. te.getName(), te.getModTime(),
  96. te.isDirectory(), mapper);
  97. }
  98. log("expand complete", Project.MSG_VERBOSE);
  99. } catch (IOException ioe) {
  100. throw new BuildException("Error while expanding " + srcF.getPath(),
  101. ioe, getLocation());
  102. } finally {
  103. FileUtils.close(tis);
  104. }
  105. }
  106. /**
  107. * Valid Modes for Compression attribute to Untar Task
  108. *
  109. */
  110. public static final class UntarCompressionMethod
  111. extends EnumeratedAttribute {
  112. // permissible values for compression attribute
  113. /**
  114. * No compression
  115. */
  116. private static final String NONE = "none";
  117. /**
  118. * GZIP compression
  119. */
  120. private static final String GZIP = "gzip";
  121. /**
  122. * BZIP2 compression
  123. */
  124. private static final String BZIP2 = "bzip2";
  125. /**
  126. * Constructor
  127. */
  128. public UntarCompressionMethod() {
  129. super();
  130. setValue(NONE);
  131. }
  132. /**
  133. * Get valid enumeration values
  134. *
  135. * @return valid values
  136. */
  137. public String[] getValues() {
  138. return new String[] {NONE, GZIP, BZIP2};
  139. }
  140. /**
  141. * This method wraps the input stream with the
  142. * corresponding decompression method
  143. *
  144. * @param file provides location information for BuildException
  145. * @param istream input stream
  146. * @return input stream with on-the-fly decompression
  147. * @exception IOException thrown by GZIPInputStream constructor
  148. * @exception BuildException thrown if bzip stream does not
  149. * start with expected magic values
  150. */
  151. private InputStream decompress(final File file,
  152. final InputStream istream)
  153. throws IOException, BuildException {
  154. final String v = getValue();
  155. if (GZIP.equals(v)) {
  156. return new GZIPInputStream(istream);
  157. } else {
  158. if (BZIP2.equals(v)) {
  159. final char[] magic = new char[] {'B', 'Z'};
  160. for (int i = 0; i < magic.length; i++) {
  161. if (istream.read() != magic[i]) {
  162. throw new BuildException(
  163. "Invalid bz2 file." + file.toString());
  164. }
  165. }
  166. return new CBZip2InputStream(istream);
  167. }
  168. }
  169. return istream;
  170. }
  171. }
  172. }