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 * 060 * @param name the compressor name. One of "gz", "bzip2", "xz", "pack200", or "deflate". 061 * @param source file to compress, may not be null. 062 * @param destination compressed file, may not be null. 063 * @param deleteSource if true, attempt to delete file on completion. Failure to delete does not cause an exception 064 * to be thrown or affect return value. 065 */ 066 public CommonsCompressAction(final String name, final File source, final File destination, 067 final boolean deleteSource) { 068 Objects.requireNonNull(source, "source"); 069 Objects.requireNonNull(destination, "destination"); 070 this.name = name; 071 this.source = source; 072 this.destination = destination; 073 this.deleteSource = deleteSource; 074 } 075 076 /** 077 * Compresses. 078 * 079 * @return true if successfully compressed. 080 * @throws IOException on IO exception. 081 */ 082 @Override 083 public boolean execute() throws IOException { 084 return execute(name, source, destination, deleteSource); 085 } 086 087 /** 088 * Compresses a file. 089 * 090 * @param name the compressor name, i.e. "gz", "bzip2", "xz", "pack200", or "deflate". 091 * @param source file to compress, may not be null. 092 * @param destination compressed file, may not be null. 093 * @param deleteSource if true, attempt to delete file on completion. Failure to delete does not cause an exception 094 * to be thrown or affect return value. 095 * 096 * @return true if source file compressed. 097 * @throws IOException on IO exception. 098 */ 099 public static boolean execute(final String name, final File source, final File destination, 100 final boolean deleteSource) throws IOException { 101 if (!source.exists()) { 102 return false; 103 } 104 try (final FileInputStream input = new FileInputStream(source); 105 final BufferedOutputStream output = new BufferedOutputStream( 106 new CompressorStreamFactory().createCompressorOutputStream(name, new FileOutputStream( 107 destination)))) { 108 IOUtils.copy(input, output, BUF_SIZE); 109 } catch (final CompressorException e) { 110 throw new IOException(e); 111 } 112 113 if (deleteSource && !source.delete()) { 114 LOGGER.warn("Unable to delete " + source.toString() + '.'); 115 } 116 return true; 117 } 118 119 /** 120 * Reports exception. 121 * 122 * @param ex exception. 123 */ 124 @Override 125 protected void reportException(final Exception ex) { 126 LOGGER.warn("Exception during " + name + " compression of '" + source.toString() + "'.", ex); 127 } 128 129 @Override 130 public String toString() { 131 return CommonsCompressAction.class.getSimpleName() + '[' + source + " to " + destination // 132 + ", deleteSource=" + deleteSource + ']'; 133 } 134 135 public String getName() { 136 return name; 137 } 138 139 public File getSource() { 140 return source; 141 } 142 143 public File getDestination() { 144 return destination; 145 } 146 147 public boolean isDeleteSource() { 148 return deleteSource; 149 } 150}