/src/main/java/com/pronetbeans/bamboo/filezip/ZipFileVisitor.java

https://bitbucket.org/christopinka/bamboo-zip-file-tasks · Java · 171 lines · 106 code · 33 blank · 32 comment · 13 complexity · 0fa2264bbe5e7e32e85fcc24bd2df17b MD5 · raw file

  1. package com.pronetbeans.bamboo.filezip;
  2. import com.atlassian.bamboo.build.logger.BuildLogger;
  3. import com.atlassian.bamboo.utils.FileVisitor;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8. import org.apache.commons.lang.StringUtils;
  9. import org.apache.log4j.Logger;
  10. import org.apache.tools.ant.DirectoryScanner;
  11. import org.apache.tools.ant.Project;
  12. import org.apache.tools.ant.types.FileSet;
  13. import org.apache.tools.ant.types.PatternSet;
  14. public class ZipFileVisitor extends FileVisitor {
  15. private File myRootDirectory = null;
  16. private static final Logger log = Logger.getLogger(ZipFileVisitor.class);
  17. private BuildLogger buildLogger;
  18. private ZipOutputStream out;
  19. private String propsZipLocation;
  20. /**
  21. * Visit file in the root directory
  22. *
  23. * @param rootDirectory
  24. */
  25. protected ZipFileVisitor(File rootDirectory) {
  26. super(rootDirectory);
  27. this.myRootDirectory = rootDirectory;
  28. }
  29. @Override
  30. public void visitFilesThatMatch(String filePattern) throws InterruptedException {
  31. String[] patterns = filePattern.split(",");
  32. for (String pattern1 : patterns) {
  33. if (Thread.currentThread().isInterrupted()) {
  34. throw new InterruptedException();
  35. }
  36. String pattern = StringUtils.trimToEmpty(pattern1);
  37. File patterFile = new File(myRootDirectory, pattern);
  38. if (patterFile.isDirectory()) {
  39. visitAllFilesInADirectory(patterFile);
  40. } else {
  41. visitAllFilesThatMatchAPattern(pattern);
  42. }
  43. }
  44. }
  45. public void visitAllFilesInADirectory(File patterFile) throws InterruptedException {
  46. File[] files = patterFile.listFiles();
  47. if (files != null) {
  48. for (File file : files) {
  49. visitFile(file);
  50. }
  51. }
  52. }
  53. /**
  54. *
  55. * @param pattern
  56. * @throws InterruptedException
  57. */
  58. public void visitAllFilesThatMatchAPattern(String pattern) throws InterruptedException {
  59. FileSet fileSet = new FileSet();
  60. fileSet.setDir(myRootDirectory);
  61. // ensure the ZIP file being created is EXCLUDED if it is under the same src working directory.
  62. // Otherwise the ZIP created is added by into the ZIP file recursively until your
  63. // disk is full
  64. PatternSet.NameEntry exclude2 = fileSet.createExclude();
  65. exclude2.setName(propsZipLocation);
  66. // exclude the "build-number.txt" fiel that is automatically in each build dir (this is a
  67. // file used by Bamboo
  68. PatternSet.NameEntry exclude = fileSet.createExclude();
  69. exclude.setName("build-number.txt");
  70. PatternSet.NameEntry include = fileSet.createInclude();
  71. include.setName(pattern);
  72. DirectoryScanner ds = fileSet.getDirectoryScanner(new Project());
  73. String[] srcFiles = ds.getIncludedFiles();
  74. for (String srcFile : srcFiles) {
  75. visitFile(new File(myRootDirectory, srcFile));
  76. }
  77. String[] srcDirs = ds.getIncludedDirectories();
  78. for (String srcDir : srcDirs) {
  79. visitFile(new File(myRootDirectory, srcDir));
  80. }
  81. }
  82. @Override
  83. public void visitFile(File file) throws InterruptedException {
  84. if (file.isDirectory()) {
  85. try {
  86. // Add ZIP entry to output stream.
  87. //out.putNextEntry(new ZipEntry(file.getName()));
  88. } catch (Exception e) {
  89. log.info(Utils.getLogBanner());
  90. buildLogger.addErrorLogEntry("Error adding zip entry : " + e.getMessage(), e);
  91. }
  92. } else if (file.isFile()) {
  93. try {
  94. final String filePath = file.getAbsolutePath();
  95. FileInputStream in = new FileInputStream(filePath);
  96. String trunNameBelowWorkingDir = filePath.substring((int) myRootDirectory.getAbsolutePath().length() + 1);
  97. // Add ZIP entry to output stream.
  98. out.putNextEntry(new ZipEntry(trunNameBelowWorkingDir));
  99. // Transfer bytes from the file to the ZIP file
  100. byte[] buf = new byte[1024];
  101. int len;
  102. while ((len = in.read(buf)) > 0) {
  103. out.write(buf, 0, len);
  104. }
  105. // Complete the entry
  106. out.closeEntry();
  107. in.close();
  108. } catch (Exception e) {
  109. log.info(Utils.getLogBanner());
  110. buildLogger.addErrorLogEntry("Failed to add file : " + e.getMessage(), e);
  111. }
  112. }
  113. }
  114. /**
  115. * @return the buildLogger
  116. */
  117. public BuildLogger getBuildLogger() {
  118. return buildLogger;
  119. }
  120. /**
  121. * @param buildLogger the buildLogger to set
  122. */
  123. public void setBuildLogger(BuildLogger buildLogger) {
  124. this.buildLogger = buildLogger;
  125. }
  126. /**
  127. * @param out the out to set
  128. */
  129. public void setOut(ZipOutputStream out) {
  130. this.out = out;
  131. }
  132. /**
  133. * @param propsZipLocation the propsZipLocation to set
  134. */
  135. public void setPropsZipLocation(String propsZipLocation) {
  136. this.propsZipLocation = propsZipLocation;
  137. }
  138. }