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