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.helper;
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
25 import java.util.zip.GZIPOutputStream;
26
27
28
29
30 public final class GZCompressAction extends AbstractAction {
31
32 private static final int BUF_SIZE = 8102;
33
34
35
36
37 private final File source;
38
39
40
41
42 private final File destination;
43
44
45
46
47 private final boolean deleteSource;
48
49
50
51
52
53
54
55
56
57 public GZCompressAction(final File source, final File destination, final boolean deleteSource) {
58 if (source == null) {
59 throw new NullPointerException("source");
60 }
61
62 if (destination == null) {
63 throw new NullPointerException("destination");
64 }
65
66 this.source = source;
67 this.destination = destination;
68 this.deleteSource = deleteSource;
69 }
70
71
72
73
74
75
76
77 @Override
78 public boolean execute() throws IOException {
79 return execute(source, destination, deleteSource);
80 }
81
82
83
84
85
86
87
88
89
90
91
92 public static boolean execute(final File source, final File destination, final boolean deleteSource)
93 throws IOException {
94 if (source.exists()) {
95 final FileInputStream fis = new FileInputStream(source);
96 final FileOutputStream fos = new FileOutputStream(destination);
97 final GZIPOutputStream gzos = new GZIPOutputStream(fos);
98 final BufferedOutputStream os = new BufferedOutputStream(gzos);
99 final byte[] inbuf = new byte[BUF_SIZE];
100 int n;
101
102 while ((n = fis.read(inbuf)) != -1) {
103 os.write(inbuf, 0, n);
104 }
105
106 os.close();
107 fis.close();
108
109 if (deleteSource && !source.delete()) {
110 LOGGER.warn("Unable to delete " + source.toString() + '.');
111 }
112
113 return true;
114 }
115
116 return false;
117 }
118
119
120
121
122
123
124
125 @Override
126 protected void reportException(final Exception ex) {
127 LOGGER.warn("Exception during compression of '" + source.toString() + "'.", ex);
128 }
129
130 }