View Javadoc

1   package com.atlassian.core.util.zip;
2   
3   import javax.annotation.Nonnull;
4   import java.io.File;
5   import java.io.IOException;
6   
7   /**
8    * This class is able to append directory recursively to the archive related to provided
9    * {@link com.atlassian.core.util.zip.FileArchiver}
10   *
11   * @since v6.4
12   */
13  public class FolderAppender
14  {
15      private final FileArchiver fileArchiver;
16      private final File archiveFile;
17      private final File rootFolderToCompress;
18      private final ArchiveParams archiveParams;
19  
20      /**
21       *
22       * @param fileArchiver the {@link com.atlassian.core.util.zip.FileArchiver}
23       * @param rootFolderToCompress handle to directory which will be appended to archive with all subdirectories recursively
24       * @param targetRootArchivePath the path used as target directory within archive, so all files and directories inside
25       * archive will start with this path
26       * @param archiveFile handle to result archive file, needed to avoid compressing himself
27       */
28      public FolderAppender(final FileArchiver fileArchiver, final File rootFolderToCompress, final String targetRootArchivePath,
29              final File archiveFile)
30      {
31          this.fileArchiver = fileArchiver;
32          this.rootFolderToCompress = rootFolderToCompress;
33          this.archiveParams = ArchiveParams.builder().withArchiveFolderName(targetRootArchivePath).build();
34          this.archiveFile = archiveFile;
35      }
36  
37      /**
38       * @param fileArchiver          the {@link com.atlassian.core.util.zip.FileArchiver}
39       * @param rootFolderToCompress  handle to directory which will be appended to archive with all subdirectories
40       *                              recursively
41       * @param archiveParams         parameters for the files in the folder to be added the to archive
42       */
43      public FolderAppender(final FileArchiver fileArchiver, final File rootFolderToCompress, final ArchiveParams archiveParams,
44                            final File archiveFile)
45      {
46          this.fileArchiver = fileArchiver;
47          this.rootFolderToCompress = rootFolderToCompress;
48          this.archiveParams = archiveParams;
49          this.archiveFile = archiveFile;
50      }
51  
52      /**
53       * Appends file or directory based on parameters used to construct the {@link com.atlassian.core.util.zip.FolderAppender}
54       * object.
55       * @throws IOException
56       */
57      public void append() throws IOException
58      {
59          append(rootFolderToCompress);
60      }
61  
62      private void append(final File file) throws IOException
63      {
64          if ((file != null) && file.exists() && (archiveParams.isIncludeHiddenFiles() || !file.isHidden()))
65          {
66              if (file.isFile())
67              {
68                  appendFile(file);
69              }
70              else if (file.isDirectory())
71              {
72                  appendFolder(file);
73              }
74          }
75      }
76  
77      private void appendFolder(@Nonnull final File file) throws IOException
78      {
79          fileArchiver.addDirectoryToArchive(file, getPathInsideArchive(file), archiveParams.getArchiveFolderName());
80          final File[] files = file.listFiles();
81          if (files != null)
82          {
83              for (final File f : files)
84              {
85                  append(f);
86              }
87          }
88      }
89  
90      private void appendFile(@Nonnull final File file) throws IOException
91      {
92          if (file.isFile() && !file.equals(archiveFile))
93          {
94              fileArchiver.addToArchive(file, getPathInsideArchive(file), archiveParams.getArchiveFolderName());
95          }
96      }
97  
98  
99      /**
100      * Builds a file path for an entry inside of the archive
101      *
102      * @param file file to add to archive
103      * @return related path inside of the archive
104      */
105     private String getPathInsideArchive(@Nonnull final File file)
106     {
107         return file.getPath().substring(rootFolderToCompress.getPath().length());
108     }
109 }