View Javadoc

1   /*
2    * Copyright 1999,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j.rolling.helper;
18  
19  import org.apache.log4j.helpers.LogLog;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  
26  import java.util.zip.ZipEntry;
27  import java.util.zip.ZipOutputStream;
28  
29  
30  /***
31   * Compresses a file using Zip compression.
32   *
33   * @author Curt Arnold
34   */
35  public final class ZipCompressAction extends ActionBase {
36    /***
37     * Source file.
38     */
39    private final File source;
40  
41    /***
42     * Destination file.
43     */
44    private final File destination;
45  
46    /***
47     * If true, attempt to delete file on completion.
48     */
49    private final boolean deleteSource;
50  
51  
52    /***
53     * Create new instance of GZCompressAction.
54     *
55     * @param source file to compress, may not be null.
56     * @param destination compressed file, may not be null.
57     * @param deleteSource if true, attempt to delete file on completion.  Failure to delete
58     * does not cause an exception to be thrown or affect return value.
59     */
60    public ZipCompressAction(
61      final File source, final File destination, final boolean deleteSource) {
62      if (source == null) {
63        throw new NullPointerException("source");
64      }
65  
66      if (destination == null) {
67        throw new NullPointerException("destination");
68      }
69  
70      this.source = source;
71      this.destination = destination;
72      this.deleteSource = deleteSource;
73    }
74  
75    /***
76     * Compress.
77     * @return true if successfully compressed.
78     * @throws IOException on IO exception.
79     */
80    public boolean execute() throws IOException {
81      return execute(source, destination, deleteSource);
82    }
83  
84    /***
85     * Compress a file.
86     *
87     * @param source file to compress, may not be null.
88     * @param destination compressed file, may not be null.
89     * @param deleteSource if true, attempt to delete file on completion.  Failure to delete
90     * does not cause an exception to be thrown or affect return value.
91     * @return true if source file compressed.
92     * @throws IOException on IO exception.
93     */
94    public static boolean execute(
95      final File source, final File destination, final boolean deleteSource)
96            throws IOException {
97      if (source.exists()) {
98        FileInputStream fis = new FileInputStream(source);
99        FileOutputStream fos = new FileOutputStream(destination);
100       ZipOutputStream zos = new ZipOutputStream(fos);
101 
102       ZipEntry zipEntry = new ZipEntry(source.getName());
103       zos.putNextEntry(zipEntry);
104 
105       byte[] inbuf = new byte[8102];
106       int n;
107 
108       while ((n = fis.read(inbuf)) != -1) {
109         zos.write(inbuf, 0, n);
110       }
111 
112       zos.close();
113       fis.close();
114 
115       if (deleteSource && !source.delete()) {
116           LogLog.warn("Unable to delete " + source.toString() + ".");
117       }
118 
119       return true;
120     }
121 
122     return false;
123   }
124 
125     /***
126      * Capture exception.
127      *
128      * @param ex exception.
129      */
130     protected void reportException(final Exception ex) {
131             LogLog.warn("Exception during compression of '" + source.toString() + "'.", ex);
132     }
133 }