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