22
22
import java .util .zip .ZipEntry ;
23
23
import java .util .zip .ZipOutputStream ;
24
24
25
+ import ch .qos .logback .core .rolling .RolloverFailure ;
25
26
import ch .qos .logback .core .spi .ContextAwareBase ;
26
27
import ch .qos .logback .core .status .ErrorStatus ;
27
28
import ch .qos .logback .core .status .WarnStatus ;
29
+ import ch .qos .logback .core .util .FileUtil ;
28
30
29
31
/**
30
32
* The <code>Compression</code> class implements ZIP and GZ file
31
33
* compression/decompression methods.
32
- *
34
+ *
33
35
* @author Ceki Gülcü
34
36
*/
35
37
public class Compressor extends ContextAwareBase {
@@ -40,25 +42,22 @@ public Compressor(CompressionMode compressionMode) {
40
42
this .compressionMode = compressionMode ;
41
43
}
42
44
43
- /**
44
- *
45
- * @param nameOfFile2Compress
46
- * @param nameOfCompressedFile
47
- * @param innerEntryName The name of the file within the zip file. Use for ZIP compression.
48
- */
45
+ /**
46
+ * @param nameOfFile2Compress
47
+ * @param nameOfCompressedFile
48
+ * @param innerEntryName The name of the file within the zip file. Use for ZIP compression.
49
+ */
49
50
public void compress (String nameOfFile2Compress , String nameOfCompressedFile , String innerEntryName ) {
50
51
switch (compressionMode ) {
51
- case GZ :
52
- addInfo ("GZ compressing [" + nameOfFile2Compress + "]." );
53
- gzCompress (nameOfFile2Compress , nameOfCompressedFile );
54
- break ;
55
- case ZIP :
56
- addInfo ("ZIP compressing [" + nameOfFile2Compress + "]." );
57
- zipCompress (nameOfFile2Compress , nameOfCompressedFile , innerEntryName );
58
- break ;
59
- case NONE :
60
- throw new UnsupportedOperationException (
61
- "compress method called in NONE compression mode" );
52
+ case GZ :
53
+ gzCompress (nameOfFile2Compress , nameOfCompressedFile );
54
+ break ;
55
+ case ZIP :
56
+ zipCompress (nameOfFile2Compress , nameOfCompressedFile , innerEntryName );
57
+ break ;
58
+ case NONE :
59
+ throw new UnsupportedOperationException (
60
+ "compress method called in NONE compression mode" );
62
61
}
63
62
}
64
63
@@ -67,12 +66,12 @@ private void zipCompress(String nameOfFile2zip, String nameOfZippedFile, String
67
66
68
67
if (!file2zip .exists ()) {
69
68
addStatus (new WarnStatus ("The file to compress named [" + nameOfFile2zip
70
- + "] does not exist." , this ));
69
+ + "] does not exist." , this ));
71
70
72
71
return ;
73
72
}
74
73
75
- if (innerEntryName == null ) {
74
+ if (innerEntryName == null ) {
76
75
addStatus (new WarnStatus ("The innerEntryName parameter cannot be null" , this ));
77
76
return ;
78
77
}
@@ -85,11 +84,14 @@ private void zipCompress(String nameOfFile2zip, String nameOfZippedFile, String
85
84
86
85
if (zippedFile .exists ()) {
87
86
addStatus (new WarnStatus ("The target compressed file named ["
88
- + nameOfZippedFile + "] exist already." , this ));
87
+ + nameOfZippedFile + "] exist already." , this ));
89
88
90
89
return ;
91
90
}
92
91
92
+ addInfo ("ZIP compressing [" + file2zip + "] as [" +zippedFile +"]" );
93
+ createMissingTargetDirsIfNecessary (zippedFile );
94
+
93
95
BufferedInputStream bis = null ;
94
96
ZipOutputStream zos = null ;
95
97
try {
@@ -113,20 +115,20 @@ private void zipCompress(String nameOfFile2zip, String nameOfZippedFile, String
113
115
114
116
if (!file2zip .delete ()) {
115
117
addStatus (new WarnStatus ("Could not delete [" + nameOfFile2zip + "]." ,
116
- this ));
118
+ this ));
117
119
}
118
120
} catch (Exception e ) {
119
121
addStatus (new ErrorStatus ("Error occurred while compressing ["
120
- + nameOfFile2zip + "] into [" + nameOfZippedFile + "]." , this , e ));
122
+ + nameOfFile2zip + "] into [" + nameOfZippedFile + "]." , this , e ));
121
123
} finally {
122
- if (bis != null ) {
124
+ if (bis != null ) {
123
125
try {
124
126
bis .close ();
125
127
} catch (IOException e ) {
126
128
// ignore
127
129
}
128
130
}
129
- if (zos != null ) {
131
+ if (zos != null ) {
130
132
try {
131
133
zos .close ();
132
134
} catch (IOException e ) {
@@ -168,24 +170,27 @@ private void gzCompress(String nameOfFile2gz, String nameOfgzedFile) {
168
170
169
171
if (!file2gz .exists ()) {
170
172
addStatus (new WarnStatus ("The file to compress named [" + nameOfFile2gz
171
- + "] does not exist." , this ));
173
+ + "] does not exist." , this ));
172
174
173
175
return ;
174
176
}
175
177
178
+
176
179
if (!nameOfgzedFile .endsWith (".gz" )) {
177
180
nameOfgzedFile = nameOfgzedFile + ".gz" ;
178
181
}
179
182
180
183
File gzedFile = new File (nameOfgzedFile );
181
184
182
185
if (gzedFile .exists ()) {
183
- addStatus (new WarnStatus ("The target compressed file named ["
184
- + nameOfgzedFile + "] exist already." , this ));
185
-
186
+ addWarn ("The target compressed file named ["
187
+ + nameOfgzedFile + "] exist already. Aborting file compression." );
186
188
return ;
187
189
}
188
190
191
+ addInfo ("GZ compressing [" + file2gz + "] as [" +gzedFile +"]" );
192
+ createMissingTargetDirsIfNecessary (gzedFile );
193
+
189
194
BufferedInputStream bis = null ;
190
195
GZIPOutputStream gzos = null ;
191
196
try {
@@ -205,20 +210,20 @@ private void gzCompress(String nameOfFile2gz, String nameOfgzedFile) {
205
210
206
211
if (!file2gz .delete ()) {
207
212
addStatus (new WarnStatus ("Could not delete [" + nameOfFile2gz + "]." ,
208
- this ));
213
+ this ));
209
214
}
210
215
} catch (Exception e ) {
211
216
addStatus (new ErrorStatus ("Error occurred while compressing ["
212
- + nameOfFile2gz + "] into [" + nameOfgzedFile + "]." , this , e ));
217
+ + nameOfFile2gz + "] into [" + nameOfgzedFile + "]." , this , e ));
213
218
} finally {
214
- if (bis != null ) {
219
+ if (bis != null ) {
215
220
try {
216
221
bis .close ();
217
222
} catch (IOException e ) {
218
223
// ignore
219
224
}
220
225
}
221
- if (gzos != null ) {
226
+ if (gzos != null ) {
222
227
try {
223
228
gzos .close ();
224
229
} catch (IOException e ) {
@@ -233,24 +238,38 @@ static public String computeFileNameStr_WCS(String fileNamePatternStr,
233
238
int len = fileNamePatternStr .length ();
234
239
switch (compressionMode ) {
235
240
case GZ :
236
- if (fileNamePatternStr .endsWith (".gz" ))
237
- return fileNamePatternStr .substring (0 , len - 3 );
238
- else
239
- return fileNamePatternStr ;
241
+ if (fileNamePatternStr .endsWith (".gz" ))
242
+ return fileNamePatternStr .substring (0 , len - 3 );
243
+ else
244
+ return fileNamePatternStr ;
240
245
case ZIP :
241
- if (fileNamePatternStr .endsWith (".zip" ))
242
- return fileNamePatternStr .substring (0 , len - 4 );
243
- else
244
- return fileNamePatternStr ;
246
+ if (fileNamePatternStr .endsWith (".zip" ))
247
+ return fileNamePatternStr .substring (0 , len - 4 );
248
+ else
249
+ return fileNamePatternStr ;
245
250
case NONE :
246
251
return fileNamePatternStr ;
247
252
}
248
253
throw new IllegalStateException ("Execution should not reach this point" );
249
254
}
250
255
256
+
257
+ void createMissingTargetDirsIfNecessary (File file ) {
258
+ if (FileUtil .isParentDirectoryCreationRequired (file )) {
259
+ boolean result = FileUtil .createMissingParentDirectories (file );
260
+ if (!result ) {
261
+ addError ("Failed to create parent directories for ["
262
+ + file .getAbsolutePath () + "]" );
263
+ } else {
264
+ addInfo ("Created missing parent directories for ["
265
+ + file .getAbsolutePath () + "]" );
266
+ }
267
+ }
268
+ }
269
+
251
270
@ Override
252
271
public String toString () {
253
- return "c.q.l.core.rolling.helper.Compress" ;
272
+ return this . getClass (). getName () ;
254
273
}
255
274
256
275
}
0 commit comments