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.

BUnzip2.java 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * @author Magesh Umasankar
  30. *
  31. * @since Ant 1.5
  32. *
  33. * @ant.task category="packaging"
  34. */
  35. public class BUnzip2 extends Unpack {
  36. private static final String DEFAULT_EXTENSION = ".bz2";
  37. protected String getDefaultExtension() {
  38. return DEFAULT_EXTENSION;
  39. }
  40. protected void extract() {
  41. if (source.lastModified() > dest.lastModified()) {
  42. log("Expanding " + source.getAbsolutePath() + " to "
  43. + dest.getAbsolutePath());
  44. FileOutputStream out = null;
  45. CBZip2InputStream zIn = null;
  46. FileInputStream fis = null;
  47. BufferedInputStream bis = null;
  48. try {
  49. out = new FileOutputStream(dest);
  50. fis = new FileInputStream(source);
  51. bis = new BufferedInputStream(fis);
  52. int b = bis.read();
  53. if (b != 'B') {
  54. throw new BuildException("Invalid bz2 file.", getLocation());
  55. }
  56. b = bis.read();
  57. if (b != 'Z') {
  58. throw new BuildException("Invalid bz2 file.", getLocation());
  59. }
  60. zIn = new CBZip2InputStream(bis);
  61. byte[] buffer = new byte[8 * 1024];
  62. int count = 0;
  63. do {
  64. out.write(buffer, 0, count);
  65. count = zIn.read(buffer, 0, buffer.length);
  66. } while (count != -1);
  67. } catch (IOException ioe) {
  68. String msg = "Problem expanding bzip2 " + ioe.getMessage();
  69. throw new BuildException(msg, ioe, getLocation());
  70. } finally {
  71. if (bis != null) {
  72. try {
  73. bis.close();
  74. } catch (IOException ioex) {
  75. // ignore
  76. }
  77. }
  78. if (fis != null) {
  79. try {
  80. fis.close();
  81. } catch (IOException ioex) {
  82. // ignore
  83. }
  84. }
  85. if (out != null) {
  86. try {
  87. out.close();
  88. } catch (IOException ioex) {
  89. // ignore
  90. }
  91. }
  92. if (zIn != null) {
  93. try {
  94. zIn.close();
  95. } catch (IOException ioex) {
  96. // ignore
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }