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.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.BufferedInputStream;
  56. import java.io.File;
  57. import java.io.FileInputStream;
  58. import java.io.IOException;
  59. import java.io.InputStream;
  60. import java.util.zip.GZIPInputStream;
  61. import org.apache.tools.ant.BuildException;
  62. import org.apache.tools.ant.Project;
  63. import org.apache.tools.ant.types.EnumeratedAttribute;
  64. import org.apache.tools.ant.util.FileUtils;
  65. import org.apache.tools.bzip2.CBZip2InputStream;
  66. import org.apache.tools.tar.TarEntry;
  67. import org.apache.tools.tar.TarInputStream;
  68. /**
  69. * Untar a file.
  70. * <p>For JDK 1.1 &quot;last modified time&quot; field is set to current time instead of being
  71. * carried from the archive file.</p>
  72. * <p>PatternSets are used to select files to extract
  73. * <I>from</I> the archive. If no patternset is used, all files are extracted.
  74. * </p>
  75. * <p>FileSet>s may be used used to select archived files
  76. * to perform unarchival upon.
  77. * </p>
  78. * <p>File permissions will not be restored on extracted files.</p>
  79. * <p>The untar task recognizes the long pathname entries used by GNU tar.<p>
  80. *
  81. * @author Stefan Bodewig
  82. * @author Magesh Umasankar
  83. *
  84. * @since Ant 1.1
  85. *
  86. * @ant.task category="packaging"
  87. */
  88. public class Untar extends Expand {
  89. /**
  90. * compression method
  91. */
  92. private UntarCompressionMethod compression = new UntarCompressionMethod();
  93. /**
  94. * Set decompression algorithm to use; default=none.
  95. *
  96. * Allowable values are
  97. * <ul>
  98. * <li>none - no compression
  99. * <li>gzip - Gzip compression
  100. * <li>bzip2 - Bzip2 compression
  101. * </ul>
  102. *
  103. * @param method compression method
  104. */
  105. public void setCompression(UntarCompressionMethod method) {
  106. compression = method;
  107. }
  108. /**
  109. * No encoding support in Untar.
  110. *
  111. * @since Ant 1.6
  112. */
  113. public void setEncoding(String encoding) {
  114. throw new BuildException("The " + getTaskName()
  115. + " task doesn't support the encoding"
  116. + " attribute", getLocation());
  117. }
  118. protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
  119. TarInputStream tis = null;
  120. try {
  121. log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
  122. tis = new TarInputStream(
  123. compression.decompress(srcF,
  124. new BufferedInputStream(
  125. new FileInputStream(srcF))));
  126. TarEntry te = null;
  127. while ((te = tis.getNextEntry()) != null) {
  128. extractFile(fileUtils, srcF, dir, tis,
  129. te.getName(), te.getModTime(), te.isDirectory());
  130. }
  131. log("expand complete", Project.MSG_VERBOSE);
  132. } catch (IOException ioe) {
  133. throw new BuildException("Error while expanding " + srcF.getPath(),
  134. ioe, getLocation());
  135. } finally {
  136. if (tis != null) {
  137. try {
  138. tis.close();
  139. } catch (IOException e) {}
  140. }
  141. }
  142. }
  143. /**
  144. * Valid Modes for Compression attribute to Untar Task
  145. *
  146. */
  147. public static final class UntarCompressionMethod
  148. extends EnumeratedAttribute {
  149. // permissable values for compression attribute
  150. /**
  151. * No compression
  152. */
  153. private static final String NONE = "none";
  154. /**
  155. * GZIP compression
  156. */
  157. private static final String GZIP = "gzip";
  158. /**
  159. * BZIP2 compression
  160. */
  161. private static final String BZIP2 = "bzip2";
  162. /**
  163. * Constructor
  164. */
  165. public UntarCompressionMethod() {
  166. super();
  167. setValue(NONE);
  168. }
  169. /**
  170. * Get valid enumeration values
  171. *
  172. * @return valid values
  173. */
  174. public String[] getValues() {
  175. return new String[] { NONE, GZIP, BZIP2 };
  176. }
  177. /**
  178. * This method wraps the input stream with the
  179. * corresponding decompression method
  180. *
  181. * @param file provides location information for BuildException
  182. * @param istream input stream
  183. * @return input stream with on-the-fly decompression
  184. * @exception IOException thrown by GZIPInputStream constructor
  185. * @exception BuildException thrown if bzip stream does not
  186. * start with expected magic values
  187. */
  188. private InputStream decompress(final File file,
  189. final InputStream istream)
  190. throws IOException, BuildException {
  191. final String value = getValue();
  192. if (GZIP.equals(value)) {
  193. return new GZIPInputStream(istream);
  194. } else {
  195. if (BZIP2.equals(value)) {
  196. final char[] magic = new char[] { 'B', 'Z' };
  197. for (int i = 0; i < magic.length; i++) {
  198. if (istream.read() != magic[i]) {
  199. throw new BuildException(
  200. "Invalid bz2 file." + file.toString());
  201. }
  202. }
  203. return new CBZip2InputStream(istream);
  204. }
  205. }
  206. return istream;
  207. }
  208. }
  209. }