package com.docus.server;

import org.apache.tools.tar.TarInputStream;

import java.io.*;
import java.util.zip.GZIPInputStream;

/**
 * @program: JavaCode
 * @ClassName FileUtils
 * @description:
 * @author: ltcz99
 * @create: 2023-04-16
 * @Version 1.0
 **/
public class FileUtils {


    /**
     * 解压tar.gz文件到指定目录
     *
     * @param sourceDir 源文件夹
     * @param destDir   解压后的目标文件夹
     */
    public static void unTarGz(String sourceDir, String destDir) throws Exception {
        File outFile = new File(sourceDir);
        File[] files = outFile.listFiles();
        try {
            //创建输出目录
            createDirectory(destDir, null);
            TarInputStream tarIn;
            int index = 1;
            for (File file : files) {
                if (file.getName().contains("tar.gz")) {
                    tarIn = new TarInputStream(new GZIPInputStream(
                        new BufferedInputStream(new FileInputStream(file))),
                        1024 * 2);

                    String outFileName = destDir + "/" + file.getName();
                    OutputStream out = new FileOutputStream(new File(outFileName));
                    int length = 0;
                    byte[] b = new byte[2048];
                    while ((length = tarIn.read(b)) != -1) {
                        out.write(b, 0, length);
                    }
                    out.close();
                    tarIn.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 解压gz到指定的文件夹下面
     *
     * @param sourceDir
     * @param destDir
     */
    public static void unGzipFile(String sourceDir, String destDir) {
        //创建输出目录
        createDirectory(destDir, null);
        File sourceFile = new File(sourceDir);
        File[] files = sourceFile.listFiles();
        try {
            int index = 1;
            for (File file : files) {
                if (file.getName().contains("gz")) {
                    FileInputStream fin = new FileInputStream(file);
                    //建立gzip解压工作流
                    GZIPInputStream gzin = new GZIPInputStream(fin);
                    //建立解压文件输出流
                    File tmpFile = new File(destDir + "/" + index++ + ".log");
                    FileOutputStream fout = new FileOutputStream(tmpFile);
                    int length;
                    byte[] buf = new byte[2048];
                    while ((length = gzin.read(buf, 0, buf.length)) != -1) {
                        fout.write(buf, 0, length);
                    }
                    gzin.close();
                    fout.close();
                    fin.close();
                }
            }
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }

    /**
     * 读取文件到指定的文件夹下面
     *
     * @param sourceLogPath
     * @param destLogPath
     */
    public static void readFileToDestLogPath(String sourceLogPath, String destLogPath) {
        File sourceFile = new File(sourceLogPath);
        File[] files = sourceFile.listFiles();
        for (File file : files) {
            String fileName = destLogPath + "/" + file.getName();
            File destFile = new File(fileName);
            if (file.getName().contains("log") && !fileName.contains("gz")) {
                try {
                    if (destFile.exists()) {
                        destFile.delete();
                    }
                    String logFile = sourceFile + "/" + file.getName();
                    FileInputStream fis = new FileInputStream(logFile);
                    FileOutputStream fos = new FileOutputStream(destFile);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    int len = 0;
                    while ((len = bis.read()) != -1) {
                        bos.write(len);
                    }
                    bos.flush();
                    // 关闭资源
                    fis.close();
                    bis.close();
                    fos.close();
                    bos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 创建目录
     *
     * @param outputDir
     * @param subDir
     */
    public static void createDirectory(String outputDir, String subDir) {
        File file = new File(outputDir);
        //子目录不为空
        if (!(subDir == null || subDir.trim().equals(""))) {
            file = new File(outputDir + "/" + subDir);
        }
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            file.mkdirs();
        }
    }

    public static void main(String[] args) throws Exception {
        String sourceDir = "/Users/ltcz99/Downloads/templog";
        String destDir = "/Users/ltcz99/Downloads/unzip/";
        //解压.gz文件到指定的文件件下面
        unGzipFile(sourceDir, destDir);
        // 解压tar.gz文件到指定的文件夹下面
        unTarGz(sourceDir, destDir);
        //读取特定的文件到指定的文件夹下面
        readFileToDestLogPath(sourceDir, destDir);
    }
}
