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.helper;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  
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, attempt to delete file on completion.
47       */
48      private final boolean deleteSource;
49  
50      /**
51       * Create new instance of GZCompressAction.
52       *
53       * @param source       file to compress, may not be null.
54       * @param destination  compressed file, may not be null.
55       * @param deleteSource if true, attempt to delete file on completion.  Failure to delete
56       *                     does not cause an exception to be thrown or affect return value.
57       */
58      public ZipCompressAction(final File source, final File destination, final boolean deleteSource) {
59          if (source == null) {
60              throw new NullPointerException("source");
61          }
62  
63          if (destination == null) {
64              throw new NullPointerException("destination");
65          }
66  
67          this.source = source;
68          this.destination = destination;
69          this.deleteSource = deleteSource;
70      }
71  
72      /**
73       * Compress.
74       *
75       * @return true if successfully compressed.
76       * @throws IOException on IO exception.
77       */
78      @Override
79      public boolean execute() throws IOException {
80          return execute(source, destination, deleteSource);
81      }
82  
83      /**
84       * Compress a file.
85       *
86       * @param source       file to compress, may not be null.
87       * @param destination  compressed file, may not be null.
88       * @param deleteSource if true, attempt to delete file on completion.  Failure to delete
89       *                     does not cause an exception to be thrown or affect return value.
90       * @return true if source file compressed.
91       * @throws IOException on IO exception.
92       */
93      public static boolean execute(final File source, final File destination, final boolean deleteSource)
94          throws IOException {
95          if (source.exists()) {
96              final FileInputStream fis = new FileInputStream(source);
97              final FileOutputStream fos = new FileOutputStream(destination);
98              final ZipOutputStream zos = new ZipOutputStream(fos);
99  
100             final ZipEntry zipEntry = new ZipEntry(source.getName());
101             zos.putNextEntry(zipEntry);
102 
103             final byte[] inbuf = new byte[BUF_SIZE];
104             int n;
105 
106             while ((n = fis.read(inbuf)) != -1) {
107                 zos.write(inbuf, 0, n);
108             }
109 
110             zos.close();
111             fis.close();
112 
113             if (deleteSource && !source.delete()) {
114                 LOGGER.warn("Unable to delete " + source.toString() + '.');
115             }
116 
117             return true;
118         }
119 
120         return false;
121     }
122 
123     /**
124      * Capture exception.
125      *
126      * @param ex exception.
127      */
128     @Override
129     protected void reportException(final Exception ex) {
130         LOGGER.warn("Exception during compression of '" + source.toString() + "'.", ex);
131     }
132 }