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.

GUnzip.java 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.util.zip.GZIPInputStream;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.util.FileUtils;
  25. /**
  26. * Expands a file that has been compressed with the GZIP
  27. * algorithm. Normally used to compress non-compressed archives such
  28. * as TAR files.
  29. *
  30. * @since Ant 1.1
  31. *
  32. * @ant.task category="packaging"
  33. */
  34. public class GUnzip extends Unpack {
  35. private static final int BUFFER_SIZE = 8 * 1024;
  36. private static final String DEFAULT_EXTENSION = ".gz";
  37. /**
  38. * Get the default extension.
  39. * @return the value ".gz"
  40. */
  41. protected String getDefaultExtension() {
  42. return DEFAULT_EXTENSION;
  43. }
  44. /**
  45. * Implement the gunzipping.
  46. */
  47. protected void extract() {
  48. if (source.lastModified() > dest.lastModified()) {
  49. log("Expanding " + source.getAbsolutePath() + " to "
  50. + dest.getAbsolutePath());
  51. FileOutputStream out = null;
  52. GZIPInputStream zIn = null;
  53. InputStream fis = null;
  54. try {
  55. out = new FileOutputStream(dest);
  56. fis = srcResource.getInputStream();
  57. zIn = new GZIPInputStream(fis);
  58. byte[] buffer = new byte[BUFFER_SIZE];
  59. int count = 0;
  60. do {
  61. out.write(buffer, 0, count);
  62. count = zIn.read(buffer, 0, buffer.length);
  63. } while (count != -1);
  64. } catch (IOException ioe) {
  65. String msg = "Problem expanding gzip " + ioe.getMessage();
  66. throw new BuildException(msg, ioe, getLocation());
  67. } finally {
  68. FileUtils.close(fis);
  69. FileUtils.close(out);
  70. FileUtils.close(zIn);
  71. }
  72. }
  73. }
  74. /**
  75. * Whether this task can deal with non-file resources.
  76. *
  77. * <p>This implementation returns true only if this task is
  78. * &lt;gunzip&gt;. Any subclass of this class that also wants to
  79. * support non-file resources needs to override this method. We
  80. * need to do so for backwards compatibility reasons since we
  81. * can't expect subclasses to support resources.</p>
  82. * @return true if this task supports non file resources.
  83. * @since Ant 1.7
  84. */
  85. protected boolean supportsNonFileResources() {
  86. return getClass().equals(GUnzip.class);
  87. }
  88. }