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