1
2
3
4
5
6
7
8
9
10 package ch.qos.logback.core.rolling.helper;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.util.zip.GZIPOutputStream;
16 import java.util.zip.ZipEntry;
17 import java.util.zip.ZipOutputStream;
18
19 import ch.qos.logback.core.spi.ContextAwareBase;
20 import ch.qos.logback.core.status.ErrorStatus;
21 import ch.qos.logback.core.status.WarnStatus;
22
23
24
25
26
27
28
29
30 public class Compressor extends ContextAwareBase {
31
32 final CompressionMode compressionMode;
33 final String nameOfFile2Compress;
34 final String nameOfCompressedFile;
35
36
37 public Compressor(CompressionMode compressionMode, String nameOfFile2Compress, String nameOfCompressedFile) {
38 this.compressionMode = compressionMode;
39 this.nameOfFile2Compress = nameOfFile2Compress;
40 this.nameOfCompressedFile = nameOfCompressedFile;
41 }
42
43 public Compressor(CompressionMode compressionMode, String nameOfFile2Compress) {
44
45
46 this(compressionMode, nameOfFile2Compress, nameOfFile2Compress);
47 }
48
49 public void compress() {
50 switch(compressionMode) {
51 case GZ:
52 gzCompress(nameOfFile2Compress, nameOfCompressedFile);
53 break;
54 case ZIP:
55 zipCompress(nameOfFile2Compress, nameOfCompressedFile);
56 break;
57 }
58 }
59
60 private void zipCompress(String nameOfFile2zip, String nameOfZippedFile) {
61 File file2zip = new File(nameOfFile2zip);
62
63 if (!file2zip.exists()) {
64 addStatus(new WarnStatus(
65 "The file to compress named [" + nameOfFile2zip
66 + "] does not exist.", this));
67
68 return;
69 }
70
71 if (!nameOfZippedFile.endsWith(".zip")) {
72 nameOfZippedFile = nameOfZippedFile + ".zip";
73 }
74
75 File zippedFile = new File(nameOfZippedFile);
76
77 if (zippedFile.exists()) {
78 addStatus(new WarnStatus(
79 "The target compressed file named [" + nameOfZippedFile
80 + "] exist already.", this));
81
82 return;
83 }
84
85 try {
86 FileOutputStream fos = new FileOutputStream(nameOfZippedFile);
87 ZipOutputStream zos = new ZipOutputStream(fos);
88 FileInputStream fis = new FileInputStream(nameOfFile2zip);
89
90 ZipEntry zipEntry = new ZipEntry(file2zip.getName());
91 zos.putNextEntry(zipEntry);
92
93 byte[] inbuf = new byte[8102];
94 int n;
95
96 while ((n = fis.read(inbuf)) != -1) {
97 zos.write(inbuf, 0, n);
98 }
99
100 fis.close();
101 zos.close();
102
103 if (!file2zip.delete()) {
104 addStatus(new WarnStatus("Could not delete [" + nameOfFile2zip + "].",
105 this));
106 }
107 } catch (Exception e) {
108 addStatus(new ErrorStatus("Error occurred while compressing ["
109 + nameOfFile2zip + "] into [" + nameOfZippedFile + "].", this, e));
110 }
111 }
112
113 private void gzCompress(String nameOfFile2gz, String nameOfgzedFile) {
114 File file2gz = new File(nameOfFile2gz);
115
116 if (!file2gz.exists()) {
117 addStatus(new WarnStatus("The file to compress named [" + nameOfFile2gz
118 + "] does not exist.", this));
119
120 return;
121 }
122
123 if (!nameOfgzedFile.endsWith(".gz")) {
124 nameOfgzedFile = nameOfgzedFile + ".gz";
125 }
126
127 File gzedFile = new File(nameOfgzedFile);
128
129 if (gzedFile.exists()) {
130 addStatus(new WarnStatus("The target compressed file named ["
131 + nameOfgzedFile + "] exist already.", this));
132
133 return;
134 }
135
136 try {
137 FileOutputStream fos = new FileOutputStream(nameOfgzedFile);
138 GZIPOutputStream gzos = new GZIPOutputStream(fos);
139 FileInputStream fis = new FileInputStream(nameOfFile2gz);
140 byte[] inbuf = new byte[8102];
141 int n;
142
143 while ((n = fis.read(inbuf)) != -1) {
144 gzos.write(inbuf, 0, n);
145 }
146
147 fis.close();
148 gzos.close();
149
150 if (!file2gz.delete()) {
151 addStatus(new WarnStatus("Could not delete [" + nameOfFile2gz + "].",
152 this));
153 }
154 } catch (Exception e) {
155 addStatus(new ErrorStatus("Error occurred while compressing ["
156 + nameOfFile2gz + "] into [" + nameOfgzedFile + "].", this, e));
157 }
158 }
159
160 @Override
161 public String toString() {
162 return "c.q.l.core.rolling.helper.Compress";
163 }
164 }