你会去创建一个线程去处理压缩日志并删除吗?

代码如下

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,无法删除对应日志");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风清扬逍遥子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值