1. /*
  2. * Copyright 2000-2002,2004 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.FileInputStream;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.util.zip.GZIPInputStream;
  22. import org.apache.tools.ant.BuildException;
  23. /**
  24. * Expands a file that has been compressed with the GZIP
  25. * algorithm. Normally used to compress non-compressed archives such
  26. * as TAR files.
  27. *
  28. * @since Ant 1.1
  29. *
  30. * @ant.task category="packaging"
  31. */
  32. public class GUnzip extends Unpack {
  33. private static final String DEFAULT_EXTENSION = ".gz";
  34. protected String getDefaultExtension() {
  35. return DEFAULT_EXTENSION;
  36. }
  37. protected void extract() {
  38. if (source.lastModified() > dest.lastModified()) {
  39. log("Expanding " + source.getAbsolutePath() + " to "
  40. + dest.getAbsolutePath());
  41. FileOutputStream out = null;
  42. GZIPInputStream zIn = null;
  43. FileInputStream fis = null;
  44. try {
  45. out = new FileOutputStream(dest);
  46. fis = new FileInputStream(source);
  47. zIn = new GZIPInputStream(fis);
  48. byte[] buffer = new byte[8 * 1024];
  49. int count = 0;
  50. do {
  51. out.write(buffer, 0, count);
  52. count = zIn.read(buffer, 0, buffer.length);
  53. } while (count != -1);
  54. } catch (IOException ioe) {
  55. String msg = "Problem expanding gzip " + ioe.getMessage();
  56. throw new BuildException(msg, ioe, getLocation());
  57. } finally {
  58. if (fis != null) {
  59. try {
  60. fis.close();
  61. } catch (IOException ioex) {
  62. //ignore
  63. }
  64. }
  65. if (out != null) {
  66. try {
  67. out.close();
  68. } catch (IOException ioex) {
  69. //ignore
  70. }
  71. }
  72. if (zIn != null) {
  73. try {
  74. zIn.close();
  75. } catch (IOException ioex) {
  76. //ignore
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }