由于平时线上的日志文件每天都会打成tar.gz的压缩包,导致如果没有一个准确的日期,查询日志很是繁琐,需要去跳板机挨个下载,载解压查询,这个程序将后面解压,查询的功能整合,只需去跳板机将想要下载的日志下载下来即可(跳板机下载这块绕了半天没能绕过去),主要用到了apaceh.commons.compress包进行文件解压,下面贴主要代码:
package com.dong.utils.service; import com.google.common.collect.Lists; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.CompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.lang3.StringUtils; import java.io.*; import java.util.*; import java.util.concurrent.*; public class LogUtilService { private List<String> fileNames = new ArrayList<String>(); public int findLogs(String filePathStr, String filePathEx, String keyword) throws Exception { List<String> retList = Lists.newArrayList(); //1、解压缩 File filePath = new File(filePathStr); if (filePath.isDirectory()) { File[] files = filePath.listFiles(); for (File file : files) { file.getName(); if (file.getName().endsWith(".gz")) { deGzipArchive(filePathStr, filePath + File.separator + file.getName()); } } File[] filesEX = filePath.listFiles(); List<String> tarFileList = new ArrayList<>(); for (File file : filesEX) { List<String> fileList = getFilePath(file.getPath()); tarFileList.addAll(fileList); } tarFileList.forEach(tarfile -> { try { unGZ(tarfile, filePathEx); } catch (Exception e) { e.printStackTrace(); } }); } //查找关键字 File[] files = new File(filePathEx).listFiles(); int leng = files.length; ExecutorService executorService = Executors.newFixedThreadPool(leng); List<Future> futures = Lists.newArrayList(); for (File file : files) { ReadCallable task = new ReadCallable(file, keyword); try { Future future = executorService.submit(task); futures.add(future); } catch (Exception e) { e.printStackTrace(); } } for (Future future : futures) { List<String> tmpList = (List<String>) future.get(); retList.addAll(tmpList); } writeIntoFile(retList); return retList.size(); } public static void deGzipArchive(String dir, String filepath) throws Exception { final File input = new File(filepath); final InputStream is = new FileInputStream(input); final CompressorInputStream in = new GzipCompressorInputStream(is, true); TarArchiveInputStream tin = new TarArchiveInputStream(in); TarArchiveEntry entry = tin.getNextTarEntry(); while (entry != null) { File archiveEntry = new File(dir, entry.getName()); archiveEntry.getParentFile().mkdirs(); if (entry.isDirectory()) { archiveEntry.mkdir(); entry = tin.getNextTarEntry(); continue; } OutputStream out = new FileOutputStream(archiveEntry); IOUtils.copy(tin, out); out.close(); entry = tin.getNextTarEntry(); } in.close(); tin.close(); } public List<File> getFile(File file, List<File> fileList) { File[] files = file.listFiles(); if (files != null && files.length > 0) { for (File file1 : files) { if (file1.isDirectory()) { getFile(file1, fileList); } else { fileList.add(file1); return fileList; } } } return null; } public List<String> getFilePath(String path) { File file = new File(path); List<String> filePaths = new ArrayList<>(); if (file.exists()) { LinkedList<File> list = new LinkedList<File>(); if (file.isDirectory()) { File[] files = file.listFiles(); for (File file2 : files) { if (file2.isDirectory()) { list.add(file2); } } File tempFile; while (!list.isEmpty()) { tempFile = list.removeFirst(); files = tempFile.listFiles(); for (File file2 : files) { if (file2.isDirectory()) { list.add(file2); } else { if (file2.getName().endsWith("gz")) { filePaths.add(file2.getAbsolutePath()); } } } } } } return filePaths; } public List<String> unGZ(String srcFileStr, String destDir) throws IOException { File srcFile = new File(srcFileStr); if (StringUtils.isBlank(destDir)) { destDir = srcFile.getParent(); } destDir = destDir.endsWith(File.separator) ? destDir : destDir + File.separator; InputStream is = null; OutputStream os = null; try { String fileName = srcFile.getName(); if (fileNames.contains(fileName)) { fileName = fileName + "_" + 1; } File destFile = new File(destDir, fileName); fileNames.add(srcFile.getName()); is = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(srcFile), 2048)); os = new BufferedOutputStream(new FileOutputStream(destFile), 2048); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); IOUtils.closeQuietly(is); } return fileNames; } public void writeIntoFile(List<String> fileNames) { int length = fileNames.size(); if (length > 0) { File file = new File("result.txt"); try { //if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write("共匹配到:" + length); bw.newLine(); bw.write("========文件详情========:"); for (String fileName : fileNames) { bw.newLine(); bw.write(fileName); } bw.close(); } catch (IOException e) { e.printStackTrace(); } } } class ReadCallable implements Callable { private File file; private String keyword; public ReadCallable(File file, String keyword) { this.file = file; this.keyword = keyword; } @Override public List<String> call() throws Exception { List<String> retList = Lists.newArrayList(); BufferedReader reader = null; reader = new BufferedReader(new FileReader(file)); String tmpString = null; int line = 1; while ((tmpString = reader.readLine()) != null) { if (tmpString.contains(keyword)) { retList.add("文件:" + file.getName() + "; line: " + line); } line++; } reader.close(); return retList; } } }