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.Objects;
25  
26  import org.apache.commons.compress.compressors.CompressorException;
27  import org.apache.commons.compress.compressors.CompressorStreamFactory;
28  import org.apache.commons.compress.utils.IOUtils;
29  
30  /**
31   * Compresses a file using bzip2 compression.
32   */
33  public final class CommonsCompressAction extends AbstractAction {
34  
35      private static final int BUF_SIZE = 8102;
36  
37      /**
38       * Compressor name. One of "gz", "bzip2", "xz", "pack200", or "deflate".
39       */
40      private final String name;
41  
42      /**
43       * Source file.
44       */
45      private final File source;
46  
47      /**
48       * Destination file.
49       */
50      private final File destination;
51  
52      /**
53       * If true, attempt to delete file on completion.
54       */
55      private final boolean deleteSource;
56  
57      /**
58       * Creates new instance of Bzip2CompressAction.
59       * @param name the compressor name. One of "gz", "bzip2", "xz", "pack200", or "deflate".
60       * @param source file to compress, may not be null.
61       * @param destination compressed file, may not be null.
62       * @param deleteSource if true, attempt to delete file on completion. Failure to delete does not cause an exception
63       *            to be thrown or affect return value.
64       */
65      public CommonsCompressAction(final String name, final File source, final File destination, final boolean deleteSource) {
66          Objects.requireNonNull(source, "source");
67          Objects.requireNonNull(destination, "destination");
68          this.name = name;
69          this.source = source;
70          this.destination = destination;
71          this.deleteSource = deleteSource;
72      }
73  
74      /**
75       * Compresses.
76       *
77       * @return true if successfully compressed.
78       * @throws IOException on IO exception.
79       */
80      @Override
81      public boolean execute() throws IOException {
82          return execute(name, source, destination, deleteSource);
83      }
84  
85      /**
86       * Compresses a file.
87       * @param name the compressor name, i.e. "gz", "bzip2", "xz", "pack200", or "deflate".
88       * @param source file to compress, may not be null.
89       * @param destination compressed file, may not be null.
90       * @param deleteSource if true, attempt to delete file on completion. Failure to delete does not cause an exception
91       *            to be thrown or affect return value.
92       *
93       * @return true if source file compressed.
94       * @throws IOException on IO exception.
95       */
96      public static boolean execute(final String name, final File source, final File destination, final boolean deleteSource)
97              throws IOException {
98          if (!source.exists()) {
99              return false;
100         }
101         try (final FileInputStream input = new FileInputStream(source);
102                 final BufferedOutputStream output = new BufferedOutputStream(new CompressorStreamFactory()
103                         .createCompressorOutputStream(name, new FileOutputStream(destination)))) {
104             IOUtils.copy(input, output, BUF_SIZE);
105         } catch (final CompressorException e) {
106             throw new IOException(e);
107         }
108 
109         if (deleteSource && !source.delete()) {
110             LOGGER.warn("Unable to delete " + source.toString() + '.');
111         }
112         return true;
113     }
114 
115     /**
116      * Reports exception.
117      *
118      * @param ex exception.
119      */
120     @Override
121     protected void reportException(final Exception ex) {
122         LOGGER.warn("Exception during " + name + " compression of '" + source.toString() + "'.", ex);
123     }
124 
125     @Override
126     public String toString() {
127         return CommonsCompressAction.class.getSimpleName() + '[' + source + " to " + destination //
128                 + ", deleteSource=" + deleteSource + ']';
129     }
130 }