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.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.util.Objects;
24  import java.util.zip.ZipEntry;
25  import java.util.zip.ZipOutputStream;
26  
27  
28  /**
29   * Compresses a file using Zip compression.
30   */
31  public final class ZipCompressAction extends AbstractAction {
32  
33      private static final int BUF_SIZE = 8102;
34  
35      /**
36       * Source file.
37       */
38      private final File source;
39  
40      /**
41       * Destination file.
42       */
43      private final File destination;
44  
45      /**
46       * If true, attempts to delete file on completion.
47       */
48      private final boolean deleteSource;
49  
50      /**
51       * Compression level.
52       */
53      private final int level;
54  
55      /**
56       * Creates new instance of GzCompressAction.
57       *
58       * @param source       file to compress, may not be null.
59       * @param destination  compressed file, may not be null.
60       * @param deleteSource if true, attempt to delete file on completion.  Failure to delete
61       *                     does not cause an exception to be thrown or affect return value.
62       * @param level TODO
63       */
64      public ZipCompressAction(final File source, final File destination, final boolean deleteSource, final int level) {
65          Objects.requireNonNull(source, "source");
66          Objects.requireNonNull(destination, "destination");
67  
68          this.source = source;
69          this.destination = destination;
70          this.deleteSource = deleteSource;
71          this.level = level;
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(source, destination, deleteSource, level);
83      }
84  
85      /**
86       * Compresses a file.
87       *
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
91       *                     does not cause an exception to be thrown or affect return value.
92       * @param level the compression level
93       * @return true if source file compressed.
94       * @throws IOException on IO exception.
95       */
96      public static boolean execute(final File source, final File destination, final boolean deleteSource, final int level)
97              throws IOException {
98          if (source.exists()) {
99              try (final FileInputStream fis = new FileInputStream(source);
100                     final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destination))) {
101                 zos.setLevel(level);
102 
103                 final ZipEntry zipEntry = new ZipEntry(source.getName());
104                 zos.putNextEntry(zipEntry);
105 
106                 final byte[] inbuf = new byte[BUF_SIZE];
107                 int n;
108 
109                 while ((n = fis.read(inbuf)) != -1) {
110                     zos.write(inbuf, 0, n);
111                 }
112             }
113 
114             if (deleteSource && !source.delete()) {
115                 LOGGER.warn("Unable to delete " + source.toString() + '.');
116             }
117 
118             return true;
119         }
120 
121         return false;
122     }
123 
124     /**
125      * Captures exception.
126      *
127      * @param ex exception.
128      */
129     @Override
130     protected void reportException(final Exception ex) {
131         LOGGER.warn("Exception during compression of '" + source.toString() + "'.", ex);
132     }
133 
134     @Override
135     public String toString() {
136         return ZipCompressAction.class.getSimpleName() + '[' + source + " to " + destination //
137                 + ", level=" + level + ", deleteSource=" + deleteSource + ']';
138     }
139 }