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

11 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.IOException;
  20. import java.io.OutputStream;
  21. import java.nio.file.Files;
  22. import java.util.zip.GZIPInputStream;
  23. import org.apache.tools.ant.BuildException;
  24. /**
  25. * Expands a file that has been compressed with the GZIP
  26. * algorithm. Normally used to compress non-compressed archives such
  27. * as TAR files.
  28. *
  29. * @since Ant 1.1
  30. *
  31. * @ant.task category="packaging"
  32. */
  33. public class GUnzip extends Unpack {
  34. private static final int BUFFER_SIZE = 8 * 1024;
  35. private static final String DEFAULT_EXTENSION = ".gz";
  36. /**
  37. * Get the default extension.
  38. * @return the value ".gz"
  39. */
  40. @Override
  41. protected String getDefaultExtension() {
  42. return DEFAULT_EXTENSION;
  43. }
  44. /**
  45. * Implement the gunzipping.
  46. */
  47. @Override
  48. protected void extract() {
  49. if (srcResource.getLastModified() > dest.lastModified()) {
  50. log("Expanding " + srcResource.getName() + " to "
  51. + dest.getAbsolutePath());
  52. try (OutputStream out = Files.newOutputStream(dest.toPath());
  53. GZIPInputStream zIn =
  54. new GZIPInputStream(srcResource.getInputStream())) {
  55. byte[] buffer = new byte[BUFFER_SIZE];
  56. int count = 0;
  57. do {
  58. out.write(buffer, 0, count);
  59. count = zIn.read(buffer, 0, buffer.length);
  60. } while (count != -1);
  61. } catch (IOException ioe) {
  62. String msg = "Problem expanding gzip " + ioe.getMessage();
  63. throw new BuildException(msg, ioe, getLocation());
  64. }
  65. }
  66. }
  67. /**
  68. * Whether this task can deal with non-file resources.
  69. *
  70. * <p>This implementation returns true only if this task is
  71. * &lt;gunzip&gt;. Any subclass of this class that also wants to
  72. * support non-file resources needs to override this method. We
  73. * need to do so for backwards compatibility reasons since we
  74. * can't expect subclasses to support resources.</p>
  75. * @return true if this task supports non file resources.
  76. * @since Ant 1.7
  77. */
  78. @Override
  79. protected boolean supportsNonFileResources() {
  80. return getClass().equals(GUnzip.class);
  81. }
  82. }