java根据时间把多个服务的日志打包成zip,放在指定位置,返回下载URL,全用jdk实现...

本人设计思路:

第一版有点low,后期可优化成多线程处理,可提升性能

1、前端传两个参数:(startTime,endTime)

2、关于路径我是这样设计的:(downloadLog:最终下载路径,projectPath:每个服务总路径,preparePath:预备目录)

0e015cfebf684e1fd8a693378a95ea3a4c7.jpg

3、每个服务的日志名肯定都会有日期,那么我们可以把每个服务的路径放在数组中,循环处理再把每个路径中的日志名的日期提取出来进行时间筛选,把不满足要求的从数组中移除掉

79ed85704225ee1f0bee8c1d74cf0f9d985.jpg

 

 

84d6617e36c8752064bf8ff3c6ab4afe311.jpg

4、逻辑:分为四步

(1、删除预备目录所有文件)

(2、把每个服务中满足要求的日志包遍历打包到预备目录)

(3、把预备目录中所有文件包打包成一个大包到下载目录)

(4、返回下载目录 路径以及包名)

 

 

下面直接贴代码:

实现类:

/**
   * download the service log impl class 
   */

  @Override
  public String downloadLogPath(ServiceServrInfoDTO dto) {
      if (dto.getStartTime() == null || dto.getEndTime() == null) {
          throw new ServiceException(CommonStatus.illegalArgument);
      }
      log.info("The params startTime {}, endTime {}", dto.getStartTime(),dto.getEndTime());
      
      SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
      
      //project public path
      String projectPath = systemSettings.getProjectPath();
      //prepare path
      String preparePath = systemSettings.getPreparePath();
      //downloadLog path
      String downloadPath = systemSettings.getDownloadLog();
      
      List<String> projectPathList = new ArrayList<>();
      
      projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_MANAGER + "/logs");
      projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_MONITOR + "/logs");
      projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_SCHEDULER + "/logs");
      projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_COMPARATOR + "/logs");
      projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_LIBRARY + "/logs");
      projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_SNAPSHOT + "/logs");
      projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_TRANSCODER + "/logs");
      projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_BOOTSTRAP + "/logs");
      
      //the first step, is to delete the prepared directory file
      HandlerFileUtils.deleteAllFile(preparePath);
      
      //the second step, is to package each service log to the ready directory
      projectPathList.forEach(t ->{
          String fileName = t.split("/")[5] + "_" + sDateFormat.format(new Date());
          log.info("preparePath path {}, preparePath {}, fileName {}", t,preparePath,fileName);
          HandlerFileUtils.fileToZip(t,preparePath,fileName,dto);
      });
      
      //the third step, pack all the files in the ready directory into the download directory
      String fileName = "logs_" + sDateFormat.format(new Date());
      log.info("downloadPath preparePath {}, downloadPath {}, fileName {}", preparePath,downloadPath,fileName);
      HandlerFileUtils.fileToZip(preparePath, downloadPath, fileName);
        
      //the four step, return result
      String result = downloadPath + fileName + ".zip";
      log.info("download the service log return result is {}", result);
      
      return result;
  }

 

自写工具类:


package com.arcvideo.berd.face.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.arcvideo.berd.face.modules.serverinfo.ServiceServrInfoDTO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author yzhang
 *
 */
@Slf4j
public class HandlerFileUtils {
    
    /**
     * Package the file under the source FilePath directory as a zip file 
     * with a fileName name and store it in the zipFilePath path
     * 
     *
@param: @param sourceFilePath
     * @param: @param zipFilePath
     * @param: @param fileName
     * @param: @param dto
     * @param: @return 
     * @return: boolean 
     * @author: YaXin.Zhang
     * @date: 2018年7月5日 下午6:38:41
     * @since: JDK 1.8.0_101
     */

   

public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName,ServiceServrInfoDTO dto){
        boolean flag = false;
        File sourceFile = new File(sourceFilePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
        if(sourceFile.exists() == false){
            log.error("directory of files to be compressed:sourceFilePath {} not found.", sourceFilePath);
            sourceFile.mkdir(); // create path
        }

        try {
            
            File file = new File(zipFilePath);
            if(!file.exists()) {
                file.mkdirs();
            }
            
            File zipFile = new File(zipFilePath + "/" + fileName +".zip");
            if(zipFile.exists()){
                log.error("zipFilePath {} The directory exists under the name:" + fileName +".zip" +"package file.", zipFilePath);
            }else{
                
                List<File> list = new ArrayList<>();
                
                File[] sourceFiles = sourceFile.listFiles();
                for (int j = 0; j < sourceFiles.length; j++) {
                    list.add(sourceFiles[j]);
                }
                
                List<File> list2 = new ArrayList<>();
                //extract the date from the file name
                Pattern p = Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2})");
                
                for (int i = 0; i < list.size(); i++) {
                    if(list.get(i).getName().contains(".gz")) {
                        Matcher m = p.matcher(list.get(i).getName());
                        if(m.find()){
                            try {
                                Date date = sDateFormat.parse(m.group());
                                Date start =  sDateFormat.parse(dto.getStartTime());
                                Date end = sDateFormat.parse(dto.getEndTime());
                                
                                if(date.getTime() >= start.getTime() && date.getTime() <= end.getTime()) {
                                    list2.add(list.get(i));
                                }
                            } catch (ParseException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }else {
                        if(list.get(i).getName().contains("log.log")) {
                            list2.add(list.get(i));
                        }
                    }
                }

                if(null == list2 || list2.size()<1){
                    log.error("Directory of files to be compressed:sourceFilePath {} There is no file in it, no compression required.", sourceFilePath);
                }else{
                    fos = new FileOutputStream(zipFile);
                    zos = new ZipOutputStream(new BufferedOutputStream(fos));
                    byte[] bufs = new byte[1024*10];
                    for(int i=0;i<list2.size();i++){
                        //Create the ZIP entity and add it to the package
                        ZipEntry zipEntry = new ZipEntry(list2.get(i).getName());
                        zos.putNextEntry(zipEntry);
                      //Read the file to be compressed and write it into the package
                        fis = new FileInputStream(list2.get(i));
                        bis = new BufferedInputStream(fis, 1024*10);
                        int read = 0;
                        while((read=bis.read(bufs, 0, 1024*10)) != -1){
                            zos.write(bufs,0,read);
                        }
                    }
                    flag = true;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //close stream
            try {
                if(null != bis) bis.close();
                if(null != zos) zos.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return flag;
    }

    
    /**
     * Package the file under the source FilePath directory as a zip file 
     * with a fileName name and store it in the zipFilePath path
     * 
     * @param: @param sourceFilePath
     * @param: @param zipFilePath
     * @param: @param fileName
     * @param: @return 
     * @return: boolean 
     * @author: YaXin.Zhang
     * @date: 2018年7月5日 下午7:04:51
     * @since: JDK 1.8.0_101
     */

    public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
        boolean flag = false;  
        File sourceFile = new File(sourceFilePath);  
        FileInputStream fis = null;  
        BufferedInputStream bis = null;  
        FileOutputStream fos = null;  
        ZipOutputStream zos = null;  

        if(sourceFile.exists() == false){
            log.error("directory of files to be compressed:sourceFilePath {} not found.", sourceFilePath);
            sourceFile.mkdir();
        }  
        try {
            
            File file = new File(zipFilePath);
            if(!file.exists()) {
                file.mkdirs();
            }
            
            File zipFile = new File(zipFilePath + "/" + fileName +".zip");
            if(zipFile.exists()){  
                log.error("zipFilePath {} The directory exists under the name:" + fileName +".zip" +"package file.", zipFilePath);
            }else{  
                File[] sourceFiles = sourceFile.listFiles();  
                if(null == sourceFiles || sourceFiles.length<1){  
                    log.error("Directory of files to be compressed:sourceFilePath {} There is no file in it, no compression required.", sourceFilePath);
                }else{  
                    fos = new FileOutputStream(zipFile);  
                    zos = new ZipOutputStream(new BufferedOutputStream(fos));  
                    byte[] bufs = new byte[1024*10];  
                    for(int i=0;i<sourceFiles.length;i++){  
                        //Create the ZIP entity and add it to the package
                        ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());  
                        zos.putNextEntry(zipEntry);  
                        //Read the file to be compressed and write it into the package
                        fis = new FileInputStream(sourceFiles[i]);  
                        bis = new BufferedInputStream(fis, 1024*10);  
                        int read = 0;  
                        while((read=bis.read(bufs, 0, 1024*10)) != -1){  
                            zos.write(bufs,0,read);  
                        }  
                    }  
                    flag = true;  
                }  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
            throw new RuntimeException(e);  
        } catch (IOException e) {  
            e.printStackTrace();  
            throw new RuntimeException(e);  
        } finally{
            //close the stream
            try {  
                if(null != bis) bis.close();  
                if(null != zos) zos.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
                throw new RuntimeException(e);  
            }  
        }  
    return flag;  
    } 
    
    /**
     * delete all file
     * @param: @param datapath 
     * @return: void 
     * @author: YaXin.Zhang
     * @date: 2018年7月5日 下午6:35:06
     * @since: JDK 1.8.0_101
     */

    public static void deleteAllFile(String datapath) {
        File file = new File(datapath);
        if (!file.exists()) {
            return;
        }
        if (!file.isDirectory()) {
            return;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (datapath.endsWith(File.separator)) {
                temp = new File(datapath + tempList[i]);
            } else {
                temp = new File(datapath + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                deleteAllFile(datapath + "/" + tempList[i]);
                delFolder(datapath + "/" + tempList[i]);
            }
        }
        return;
    }
    
    /**
     * delete the folder
     * @param: @param folderPath
     * @return: void 
     * @author: YaXin.Zhang
     * @date: 2018年7月5日 下午6:35:49
     * @since: JDK 1.8.0_101
     */
    private static void delFolder(String folderPath) {
        try {  
            deleteAllFile(folderPath);
            String filePath = folderPath;  
            filePath = filePath.toString();  
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); // delete the null folder
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

}

 

运行日志:

The params startTime 2018-07-01, endTime 2018-07-05
preparePath path /usr/local/arcvideo/iface/face-manager/logs, preparePath /mnt/remote/prepare/, fileName face-manager_2018_07_06_11_03_27
preparePath path /usr/local/arcvideo/iface/face-monitor/logs, preparePath /mnt/remote/prepare/, fileName face-monitor_2018_07_06_11_03_27
preparePath path /usr/local/arcvideo/iface/face-scheduler/logs, preparePath /mnt/remote/prepare/, fileName face-scheduler_2018_07_06_11_03_28
preparePath path /usr/local/arcvideo/iface/face-comparator/logs, preparePath /mnt/remote/prepare/, fileName face-comparator_2018_07_06_11_03_28
preparePath path /usr/local/arcvideo/iface/face-library/logs, preparePath /mnt/remote/prepare/, fileName face-library_2018_07_06_11_03_29
preparePath path /usr/local/arcvideo/iface/face-snapshot/logs, preparePath /mnt/remote/prepare/, fileName face-snapshot_2018_07_06_11_03_30
preparePath path /usr/local/arcvideo/iface/face-transcoder/logs, preparePath /mnt/remote/prepare/, fileName face-transcoder_2018_07_06_11_03_36
preparePath path /usr/local/arcvideo/iface/face-deployer/logs, preparePath /mnt/remote/prepare/, fileName face-deployer_2018_07_06_11_03_36
downloadPath preparePath /mnt/remote/prepare/, downloadPath /mnt/remote/downloadLog/, fileName logs_2018_07_06_11_03_36
download the service log return result is /mnt/remote/downloadLog/logs_2018_07_06_11_03_36.zip

 

 

每个服务路径:

82ce5d3382f58e75cd8e3974c15d3b5a051.jpg

 

每个服务日志情况:

a561a9c7c815ec7beb7ae51abb544a35403.jpg

预备打包路径:

539b5d9d0d85f407a61e5449c4e34bc7d2a.jpg

 

下载包路径:

b6a3adc6a9c0b72d7c7fdf9a2357298c364.jpg

 

                                                                                                                                                                                   作者:YaXin.Zhang

转载于:https://my.oschina.net/zhangyaxin/blog/1841369

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值