001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.rolling.action;
018
019import java.io.BufferedOutputStream;
020import java.io.File;
021import java.io.FileInputStream;
022import java.io.FileOutputStream;
023import java.io.IOException;
024import java.util.Objects;
025
026import org.apache.commons.compress.compressors.CompressorException;
027import org.apache.commons.compress.compressors.CompressorStreamFactory;
028import org.apache.commons.compress.utils.IOUtils;
029
030/**
031 * Compresses a file using bzip2 compression.
032 */
033public final class CommonsCompressAction extends AbstractAction {
034
035    private static final int BUF_SIZE = 8102;
036
037    /**
038     * Compressor name. One of "gz", "bzip2", "xz", "pack200", or "deflate".
039     */
040    private final String name;
041
042    /**
043     * Source file.
044     */
045    private final File source;
046
047    /**
048     * Destination file.
049     */
050    private final File destination;
051
052    /**
053     * If true, attempt to delete file on completion.
054     */
055    private final boolean deleteSource;
056
057    /**
058     * Creates new instance of Bzip2CompressAction.
059     * @param name the compressor name. One of "gz", "bzip2", "xz", "pack200", or "deflate".
060     * @param source file to compress, may not be null.
061     * @param destination compressed file, may not be null.
062     * @param deleteSource if true, attempt to delete file on completion. Failure to delete does not cause an exception
063     *            to be thrown or affect return value.
064     */
065    public CommonsCompressAction(final String name, final File source, final File destination, final boolean deleteSource) {
066        Objects.requireNonNull(source, "source");
067        Objects.requireNonNull(destination, "destination");
068        this.name = name;
069        this.source = source;
070        this.destination = destination;
071        this.deleteSource = deleteSource;
072    }
073
074    /**
075     * Compresses.
076     *
077     * @return true if successfully compressed.
078     * @throws IOException on IO exception.
079     */
080    @Override
081    public boolean execute() throws IOException {
082        return execute(name, source, destination, deleteSource);
083    }
084
085    /**
086     * Compresses a file.
087     * @param name the compressor name, i.e. "gz", "bzip2", "xz", "pack200", or "deflate".
088     * @param source file to compress, may not be null.
089     * @param destination compressed file, may not be null.
090     * @param deleteSource if true, attempt to delete file on completion. Failure to delete does not cause an exception
091     *            to be thrown or affect return value.
092     *
093     * @return true if source file compressed.
094     * @throws IOException on IO exception.
095     */
096    public static boolean execute(final String name, final File source, final File destination, final boolean deleteSource)
097            throws IOException {
098        if (!source.exists()) {
099            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}