1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender.rolling.action;
18
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.util.Objects;
26
27 import org.apache.commons.compress.compressors.CompressorException;
28 import org.apache.commons.compress.compressors.CompressorStreamFactory;
29 import org.apache.commons.compress.utils.IOUtils;
30
31
32
33
34 public final class CommonsCompressAction extends AbstractAction {
35
36 private static final int BUF_SIZE = 8192;
37
38
39
40
41 private final String name;
42
43
44
45
46 private final File source;
47
48
49
50
51 private final File destination;
52
53
54
55
56 private final boolean deleteSource;
57
58
59
60
61
62
63
64
65
66
67 public CommonsCompressAction(final String name, final File source, final File destination,
68 final boolean deleteSource) {
69 Objects.requireNonNull(source, "source");
70 Objects.requireNonNull(destination, "destination");
71 this.name = name;
72 this.source = source;
73 this.destination = destination;
74 this.deleteSource = deleteSource;
75 }
76
77
78
79
80
81
82
83 @Override
84 public boolean execute() throws IOException {
85 return execute(name, source, destination, deleteSource);
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public static boolean execute(final String name, final File source, final File destination,
101 final boolean deleteSource) throws IOException {
102 if (!source.exists()) {
103 return false;
104 }
105 LOGGER.debug("Starting {} compression of {}", name, source.getPath() );
106 try (final FileInputStream input = new FileInputStream(source);
107 final BufferedOutputStream output = new BufferedOutputStream(
108 new CompressorStreamFactory().createCompressorOutputStream(name, new FileOutputStream(
109 destination)))) {
110 IOUtils.copy(input, output, BUF_SIZE);
111 LOGGER.debug("Finished {} compression of {}", name, source.getPath() );
112 } catch (final CompressorException e) {
113 throw new IOException(e);
114 }
115
116 if (deleteSource) {
117 try {
118 if (Files.deleteIfExists(source.toPath())) {
119 LOGGER.debug("Deleted {}", source.toString());
120 } else {
121 LOGGER.warn("Unable to delete {} after {} compression. File did not exist", source.toString(), name);
122 }
123 } catch (final Exception ex) {
124 LOGGER.warn("Unable to delete {} after {} compression, {}", source.toString(), name, ex.getMessage());
125 }
126 }
127
128 return true;
129 }
130
131
132
133
134
135
136 @Override
137 protected void reportException(final Exception ex) {
138 LOGGER.warn("Exception during " + name + " compression of '" + source.toString() + "'.", ex);
139 }
140
141 @Override
142 public String toString() {
143 return CommonsCompressAction.class.getSimpleName() + '[' + source + " to " + destination
144 + ", deleteSource=" + deleteSource + ']';
145 }
146
147 public String getName() {
148 return name;
149 }
150
151 public File getSource() {
152 return source;
153 }
154
155 public File getDestination() {
156 return destination;
157 }
158
159 public boolean isDeleteSource() {
160 return deleteSource;
161 }
162 }