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
9
10
11
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
23
24
25
26
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
39
40
41
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
54
55
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
101
102
103
104
105 private String getPathInsideArchive(@Nonnull final File file)
106 {
107 return file.getPath().substring(rootFolderToCompress.getPath().length());
108 }
109 }