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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 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 "The Jakarta Project", "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 org.apache.tools.ant.BuildException;
  56. import org.apache.tools.ant.Project;
  57. import org.apache.tools.tar.TarInputStream;
  58. import org.apache.tools.tar.TarEntry;
  59. import org.apache.tools.ant.util.FileUtils;
  60. import java.io.File;
  61. import java.io.FileInputStream;
  62. import java.io.BufferedInputStream;
  63. import java.io.InputStream;
  64. import java.io.IOException;
  65. import java.util.zip.GZIPInputStream;
  66. import org.apache.tools.bzip2.CBZip2InputStream;
  67. import org.apache.tools.ant.types.EnumeratedAttribute;
  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 <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  82. * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
  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. protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
  109. TarInputStream tis = null;
  110. try {
  111. log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
  112. tis = new TarInputStream(
  113. compression.decompress(srcF,
  114. new BufferedInputStream(
  115. new FileInputStream(srcF))));
  116. TarEntry te = null;
  117. while ((te = tis.getNextEntry()) != null) {
  118. extractFile(fileUtils, srcF, dir, tis,
  119. te.getName(), te.getModTime(), te.isDirectory());
  120. }
  121. log("expand complete", Project.MSG_VERBOSE);
  122. } catch (IOException ioe) {
  123. throw new BuildException("Error while expanding " + srcF.getPath(),
  124. ioe, location);
  125. } finally {
  126. if (tis != null) {
  127. try {
  128. tis.close();
  129. } catch (IOException e) {}
  130. }
  131. }
  132. }
  133. /**
  134. * Valid Modes for Compression attribute to Untar Task
  135. *
  136. */
  137. public static final class UntarCompressionMethod
  138. extends EnumeratedAttribute {
  139. // permissable values for compression attribute
  140. /**
  141. * No compression
  142. */
  143. private static final String NONE = "none";
  144. /**
  145. * GZIP compression
  146. */
  147. private static final String GZIP = "gzip";
  148. /**
  149. * BZIP2 compression
  150. */
  151. private static final String BZIP2 = "bzip2";
  152. /**
  153. * Constructor
  154. */
  155. public UntarCompressionMethod() {
  156. super();
  157. setValue(NONE);
  158. }
  159. /**
  160. * Get valid enumeration values
  161. *
  162. * @return valid values
  163. */
  164. public String[] getValues() {
  165. return new String[] { NONE, GZIP, BZIP2 };
  166. }
  167. /**
  168. * This method wraps the input stream with the
  169. * corresponding decompression method
  170. *
  171. * @param file provides location information for BuildException
  172. * @param istream input stream
  173. * @return input stream with on-the-fly decompression
  174. * @exception IOException thrown by GZIPInputStream constructor
  175. * @exception BuildException thrown if bzip stream does not
  176. * start with expected magic values
  177. */
  178. private InputStream decompress(final File file,
  179. final InputStream istream)
  180. throws IOException, BuildException {
  181. final String value = getValue();
  182. if (GZIP.equals(value)) {
  183. return new GZIPInputStream(istream);
  184. } else {
  185. if (BZIP2.equals(value)) {
  186. final char[] magic = new char[] { 'B', 'Z' };
  187. for (int i = 0; i < magic.length; i++) {
  188. if (istream.read() != magic[i]) {
  189. throw new BuildException(
  190. "Invalid bz2 file." + file.toString());
  191. }
  192. }
  193. return new CBZip2InputStream(istream);
  194. }
  195. }
  196. return istream;
  197. }
  198. }
  199. }