View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.rolling.helper;
19  
20  import org.apache.log4j.helpers.LogLog;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  
27  import java.util.zip.GZIPOutputStream;
28  
29  
30  /***
31   * Compresses a file using GZ compression.
32   *
33   * @author Curt Arnold
34   */
35  public final class GZCompressAction extends ActionBase {
36    /***
37     * Source file.
38     */
39    private final File source;
40  
41    /***
42     * Destination file.
43     */
44    private final File destination;
45  
46    /***
47     * If true, attempt to delete file on completion.
48     */
49    private final boolean deleteSource;
50  
51  
52    /***
53     * Create new instance of GZCompressAction.
54     *
55     * @param source file to compress, may not be null.
56     * @param destination compressed file, may not be null.
57     * @param deleteSource if true, attempt to delete file on completion.  Failure to delete
58     * does not cause an exception to be thrown or affect return value.
59     */
60    public GZCompressAction(
61      final File source, final File destination, final boolean deleteSource) {
62      if (source == null) {
63        throw new NullPointerException("source");
64      }
65  
66      if (destination == null) {
67        throw new NullPointerException("destination");
68      }
69  
70      this.source = source;
71      this.destination = destination;
72      this.deleteSource = deleteSource;
73    }
74  
75    /***
76     * Compress.
77     * @return true if successfully compressed.
78     * @throws IOException on IO exception.
79     */
80    public boolean execute() throws IOException {
81      return execute(source, destination, deleteSource);
82    }
83  
84    /***
85     * Compress a file.
86     *
87     * @param source file to compress, may not be null.
88     * @param destination compressed file, may not be null.
89     * @param deleteSource if true, attempt to delete file on completion.  Failure to delete
90     * does not cause an exception to be thrown or affect return value.
91     * @return true if source file compressed.
92     * @throws IOException on IO exception.
93     */
94    public static boolean execute(
95      final File source, final File destination, final boolean deleteSource)
96            throws IOException {
97      if (source.exists()) {
98        FileInputStream fis = new FileInputStream(source);
99        FileOutputStream fos = new FileOutputStream(destination);
100       GZIPOutputStream gzos = new GZIPOutputStream(fos);
101       byte[] inbuf = new byte[8102];
102       int n;
103 
104       while ((n = fis.read(inbuf)) != -1) {
105         gzos.write(inbuf, 0, n);
106       }
107 
108       gzos.close();
109       fis.close();
110 
111       if (deleteSource && !source.delete()) {
112           LogLog.warn("Unable to delete " + source.toString() + ".");
113       }
114 
115       return true;
116     }
117 
118     return false;
119   }
120 
121 
122     /***
123      * Capture exception.
124      *
125      * @param ex exception.
126      */
127     protected void reportException(final Exception ex) {
128          LogLog.warn("Exception during compression of '" + source.toString() + "'.", ex);
129     }
130 
131 }