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.io.OutputStream;
25 import java.util.Objects;
26 import java.util.zip.Deflater;
27 import java.util.zip.GZIPOutputStream;
28
29
30
31
32 public final class GzCompressAction extends AbstractAction {
33
34 private static final int BUF_SIZE = 8192;
35
36
37
38
39 private final File source;
40
41
42
43
44 private final File destination;
45
46
47
48
49 private final boolean deleteSource;
50
51
52
53
54
55
56 private final int compressionLevel;
57
58
59
60
61
62
63
64
65
66
67
68 public GzCompressAction(
69 final File source, final File destination, final boolean deleteSource, final int compressionLevel) {
70 Objects.requireNonNull(source, "source");
71 Objects.requireNonNull(destination, "destination");
72
73 this.source = source;
74 this.destination = destination;
75 this.deleteSource = deleteSource;
76 this.compressionLevel = compressionLevel;
77 }
78
79
80
81
82
83
84 @Deprecated
85 public GzCompressAction(final File source, final File destination, final boolean deleteSource) {
86 this(source, destination, deleteSource, Deflater.DEFAULT_COMPRESSION);
87 }
88
89
90
91
92
93
94
95 @Override
96 public boolean execute() throws IOException {
97 return execute(source, destination, deleteSource, compressionLevel);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 @Deprecated
112 public static boolean execute(final File source, final File destination, final boolean deleteSource)
113 throws IOException {
114 return execute(source, destination, deleteSource, Deflater.DEFAULT_COMPRESSION);
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public static boolean execute(
130 final File source,
131 final File destination,
132 final boolean deleteSource,
133 final int compressionLevel) throws IOException {
134 if (source.exists()) {
135 try (final FileInputStream fis = new FileInputStream(source);
136 final OutputStream fos = new FileOutputStream(destination);
137 final OutputStream gzipOut = new ConfigurableLevelGZIPOutputStream(
138 fos, BUF_SIZE, compressionLevel);
139
140 final OutputStream os = new BufferedOutputStream(gzipOut, BUF_SIZE)) {
141 final byte[] inbuf = new byte[BUF_SIZE];
142 int n;
143
144 while ((n = fis.read(inbuf)) != -1) {
145 os.write(inbuf, 0, n);
146 }
147 }
148
149 if (deleteSource && !source.delete()) {
150 LOGGER.warn("Unable to delete {}.", source);
151 }
152
153 return true;
154 }
155
156 return false;
157 }
158
159 private static final class ConfigurableLevelGZIPOutputStream extends GZIPOutputStream {
160
161 ConfigurableLevelGZIPOutputStream(OutputStream out, int bufSize, int level) throws IOException {
162 super(out, bufSize);
163 def.setLevel(level);
164 }
165 }
166
167
168
169
170
171
172 @Override
173 protected void reportException(final Exception ex) {
174 LOGGER.warn("Exception during compression of '" + source.toString() + "'.", ex);
175 }
176
177 @Override
178 public String toString() {
179 return GzCompressAction.class.getSimpleName() + '[' + source + " to " + destination
180 + ", deleteSource=" + deleteSource + ']';
181 }
182
183 public File getSource() {
184 return source;
185 }
186
187 public File getDestination() {
188 return destination;
189 }
190
191 public boolean isDeleteSource() {
192 return deleteSource;
193 }
194 }