1. /*
  2. * Copyright 2001-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.File;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.Task;
  25. /**
  26. * Abstract Base class for pack tasks.
  27. *
  28. *
  29. * @since Ant 1.5
  30. */
  31. public abstract class Pack extends Task {
  32. protected File zipFile;
  33. protected File source;
  34. /**
  35. * the required destination file.
  36. * @param zipFile
  37. */
  38. public void setZipfile(File zipFile) {
  39. this.zipFile = zipFile;
  40. }
  41. /**
  42. * the required destination file.
  43. * @param zipFile
  44. */
  45. public void setDestfile(File zipFile) {
  46. setZipfile(zipFile);
  47. }
  48. /**
  49. * the file to compress; required.
  50. * @param src
  51. */
  52. public void setSrc(File src) {
  53. source = src;
  54. }
  55. /**
  56. * validation routine
  57. * @throws BuildException if anything is invalid
  58. */
  59. private void validate() throws BuildException {
  60. if (zipFile == null) {
  61. throw new BuildException("zipfile attribute is required", getLocation());
  62. }
  63. if (zipFile.isDirectory()) {
  64. throw new BuildException("zipfile attribute must not "
  65. + "represent a directory!", getLocation());
  66. }
  67. if (source == null) {
  68. throw new BuildException("src attribute is required", getLocation());
  69. }
  70. if (source.isDirectory()) {
  71. throw new BuildException("Src attribute must not "
  72. + "represent a directory!", getLocation());
  73. }
  74. }
  75. /**
  76. * validate, then hand off to the subclass
  77. * @throws BuildException
  78. */
  79. public void execute() throws BuildException {
  80. validate();
  81. if (!source.exists()) {
  82. log("Nothing to do: " + source.getAbsolutePath()
  83. + " doesn't exist.");
  84. } else if (zipFile.lastModified() < source.lastModified()) {
  85. log("Building: " + zipFile.getAbsolutePath());
  86. pack();
  87. } else {
  88. log("Nothing to do: " + zipFile.getAbsolutePath()
  89. + " is up to date.");
  90. }
  91. }
  92. /**
  93. * zip a stream to an output stream
  94. * @param in
  95. * @param zOut
  96. * @throws IOException
  97. */
  98. private void zipFile(InputStream in, OutputStream zOut)
  99. throws IOException {
  100. byte[] buffer = new byte[8 * 1024];
  101. int count = 0;
  102. do {
  103. zOut.write(buffer, 0, count);
  104. count = in.read(buffer, 0, buffer.length);
  105. } while (count != -1);
  106. }
  107. /**
  108. * zip a file to an output stream
  109. * @param file
  110. * @param zOut
  111. * @throws IOException
  112. */
  113. protected void zipFile(File file, OutputStream zOut)
  114. throws IOException {
  115. FileInputStream fIn = new FileInputStream(file);
  116. try {
  117. zipFile(fIn, zOut);
  118. } finally {
  119. fIn.close();
  120. }
  121. }
  122. /**
  123. * subclasses must implement this method to do their compression
  124. */
  125. protected abstract void pack();
  126. }