代码如下
package weaver.general;;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;
public class ZipCompressThread implements Runnable {
Log log=LogFactory.getLog(this.getClass());
private String logRootPath;
private static final String ECOLOGY = "ecology";
private static final String INTEGRATION = "integration";
private static final String ECO_PREFFIX = File.separator + "ecology_";
private static final String INTERG_PREFFIX = File.separator + "integration_";
private static final String INTERG_CATE_PREFFIX = File.separator + "integration";
private static final String ZIP_SUFFIX = ".zip";
private static final String LOG_SUFFIX = ".log";
private static final String LOG = "log";
private boolean flag = false;
private int hour = 2;
private String date = getDate();
//给1小时压缩空挡
int nextHour = 3;
//logRoot: "C:\\MY_IDEA\\demo_01\\ecology\\log"
public ZipCompressThread(String logRoot) {
this.logRootPath = logRoot;
}
@Override
public void run() {
while (true) {
if (LocalTime.now().getHour() == hour) {
if (!flag) {
flag = true;
ecologyZipCompress(logRootPath);
integrationCompress(logRootPath + INTERG_CATE_PREFFIX);
}
try {
TimeUnit.MINUTES.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (LocalTime.now().getHour() == nextHour) {
flag = false;
}
}
}
}
public void ecologyZipCompress(String logRootPath) {
createFilePath(logRootPath);
}
public void integrationCompress(String interationPath) {
createFilePath(interationPath);
}
private void createFilePath(String catagoryPath) {
String filePath = null;
File catagory = new File(catagoryPath);
File[] fileList = catagory.listFiles();
if (catagory.exists()) {
for (File file : fileList) {
if (file.getName().endsWith(date + LOG_SUFFIX)) {
filePath = file.toString();
log.info("日志文件目录是:" + filePath);
//System.out.println("日志文件目录是:" + filePath);
break;
}
}
}
createZipAndDeleteLog(filePath, catagoryPath);
}
private void createZipAndDeleteLog(String filePath, String catagoryPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
Boolean isZip = false;
String target = zipTargetPath(catagoryPath);
if (null != filePath) {
//targetFile压缩的log日志全路径
File targetFile = new File(filePath);
try {
//目标zip文件(包括路径写入文件输出流)
fos = new FileOutputStream(target);
//封装进zip输出流
zos = new ZipOutputStream(fos);
if (targetFile.exists()) {
try {
//log文件建立输出流
fis = new FileInputStream(targetFile);
ZipEntry ze = new ZipEntry(targetFile.getName());
zos.putNextEntry(ze);
byte[] content = new byte[1024];
int len;
while ((len = fis.read(content)) != -1) {
zos.write(content, 0, len);
zos.flush();
}
} catch (IOException e) {
log.info(filePath + "日志压缩写入zip文件异常!");
//System.out.println(filePath + "日志压缩写入zip文件异常!");
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
log.info(filePath + "日志压缩输入流关闭异常!");
//System.out.println(filePath + "日志压缩输入流关闭异常!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
log.info(filePath + "日志压缩输出流关闭异常!");
//System.out.println(filePath + "日志压缩输出流关闭异常!");
}
}
File catagory = new File(catagoryPath);
File[] flashfileList = catagory.listFiles();
for (File file : flashfileList) {
if (file.getName().endsWith(date + ZIP_SUFFIX)) {
isZip = true;
break;
}
}
isDelete(isZip, targetFile);
} else {
if (filePath == null) {
log.info(target + "日志前一天日志已经压缩并删除!");
//System.out.println(target + "日志前一天日志已经压缩并删除!");
} else {
log.info(filePath + "日志前一天日志已经压缩并删除!");
//System.out.println(filePath + "日志前一天日志已经压缩并删除!");
}
}
}
/**
* 设置压缩的目标文件名称
*/
private String zipTargetPath(String logRoot) {
String date = getDate();
if (logRoot.endsWith(File.separator + ECOLOGY + File.separator + LOG)) {
return logRoot + ECO_PREFFIX + date + ZIP_SUFFIX;
} else {
return logRoot + INTERG_PREFFIX + date + ZIP_SUFFIX;
}
}
/**
* 获取日志20200720
*
* @return
*/
private String getDate() {
return LocalDate.now().minusDays(1).toString().replace("-", "");
}
private void isDelete(boolean isZip, File targetFile) {
if (isZip) {
if (LOG.equals(targetFile.getParentFile().getName()) && ECOLOGY.equals(targetFile.getParentFile().getParentFile().getName()) && targetFile.getName().endsWith(LOG_SUFFIX)) {
targetFile.delete();
log.info(targetFile.getName() + "前一天的日志已经删除!");
//System.out.println(targetFile.getName() + "前一天的日志已经删除!");
} else if (INTEGRATION.equals(targetFile.getParentFile().getName()) && LOG.equals(targetFile.getParentFile().getParentFile().getName()) && ECOLOGY.equals(targetFile.getParentFile().getParentFile().getParentFile().getName()) && targetFile.getName().endsWith(LOG)) {
targetFile.delete();
log.info(targetFile.getName() + "前一天的日志已经删除!");
//System.out.println(targetFile.getName() + "前一天的日志已经删除!");
} else {
log.info("该文件夹不是"+ targetFile + ",无法删除对应日志");
//System.out.println("该文件夹不是" + targetFile + ",无法删除对应日志");
}
} else {
log.info("未生成日压缩的zip,无法删除对应日志");
//System.out.println("未生成日压缩的zip,无法删除对应日志");
}
}
}