package com.tzld.piaoquan.recommend.model.produce.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author dyp
 */
@Slf4j
public class CompressUtil {
    public static void compressDirectoryToGzip(String sourceDirPath, String outputFilePath) {
        // 创建.gz文件的输出流
        try (OutputStream out = new FileOutputStream(outputFilePath);
             GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(out);
             TarArchiveOutputStream taos = new TarArchiveOutputStream(gzipOut)) {

            taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

            // 遍历目录
            Files.walk(Paths.get(sourceDirPath))
                    .filter(Files::isRegularFile)
                    .forEach(filePath -> {
                        try {
                            // 为每个文件创建TarEntry
                            TarArchiveEntry entry = new TarArchiveEntry(filePath.toFile(), filePath.toString().substring(sourceDirPath.length() + 1));
                            taos.putArchiveEntry(entry);

                            // 读取文件内容并写入TarArchiveOutputStream
                            try (InputStream is = Files.newInputStream(filePath)) {
                                byte[] buffer = new byte[1024];
                                int len;
                                while ((len = is.read(buffer)) > 0) {
                                    taos.write(buffer, 0, len);
                                }
                            }
                            // 关闭entry
                            taos.closeArchiveEntry();
                        } catch (IOException e) {
                            log.error("", e);
                        }
                    });
        } catch (Exception e) {
            log.error("", e);
        }
    }

    public static void decompressGzFile(String gzipFilePath, String destDirPath) {
        try (InputStream gzipIn = new FileInputStream(gzipFilePath);
             GzipCompressorInputStream gzIn = new GzipCompressorInputStream(gzipIn);
             TarArchiveInputStream tais = new TarArchiveInputStream(gzIn)) {

            TarArchiveEntry entry;
            Files.createDirectories(Paths.get(destDirPath));
            while ((entry = tais.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    // 如果是目录，创建目录
                    Files.createDirectories(Paths.get(destDirPath, entry.getName()));
                } else {
                    // 如果是文件，创建文件并写入内容
                    File outputFile = new File(destDirPath, entry.getName());
                    if (!outputFile.exists()) {
                        File parent = outputFile.getParentFile();
                        if (!parent.exists()) {
                            parent.mkdirs();
                        }
                        outputFile.createNewFile();
                    }
                    try (OutputStream out = new FileOutputStream(outputFile)) {
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = tais.read(buffer)) > 0) {
                            out.write(buffer, 0, len);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
