package cd.casic.server;

import cd.casic.ci.commons.properties.TargetFileUploadProperties;
import com.jcraft.jsch.*;
import jakarta.annotation.Resource;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.io.*;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@SpringBootTest(classes = {OpsServerApplication.class})
@ActiveProfiles("local")
public class ZipFileTest {
    @Resource
    TargetFileUploadProperties properties;
    String[] testFilePath = new String[]{
            "/home/ops/ops-pro/file/af4b1435-8fb0-453d-acef-eaec75c67dd8/QQ图片20250623102626.png",
            "/home/ops/ops-pro/file/7ffb5bb7-9ea2-4d8b-9e0e-b6c3f99107b5/新建 文本文档.txt",
            "/home/ops/ops-pro/file/2622340c-20db-48fc-bca2-d867da3149aa/.nodemid",
            "/home/ops/ops-pro/file/f601e6d7-050f-4c92-a353-e1db64e048e1/1 (2).txt",
            "/home/ops/ops-pro/file/9bf7c208-0576-4408-833e-e0d7eecc5a8a/office账户.txt",
            "/home/ops/ops-pro/file/332143a2-52da-4e29-8a3d-78071d4e3559/office账户.txt",
            "/home/ops/ops-pro/file/bbf03a24-5a48-4863-b1b9-c52bb00a8b0f/office账户.txt",
            "/home/ops/ops-pro/file/6ae5f778-e12c-48f2-bce1-8748e2435e26/脚本.txt",
            "/home/ops/ops-pro/file/47c98d3b-553a-4934-b4c4-f9b185eac04a/1.txt",
            "/home/ops/ops-pro/file/cafb8135-f1d1-42db-bb02-1444cda937fd/ximenzi.txt",
            "/home/ops/ops-pro/file/0e78423d-b4b6-4442-b225-e66309515d42/office账户.txt",
            "/home/ops/ops-pro/file/40831b9c-eb35-4eb9-973a-f087b48105e6/office账户.txt"
    };
    @Test
    public void testZip() throws  JSchException, SftpException {
        JSch jsch = new JSch();
        Session session = jsch.getSession(properties.getUsername(), properties.getRemoteHost(), properties.getRemotePort());
        session.setPassword(properties.getPassword());

        // 跳过 host key 检查
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        session.connect();
        ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
        sftp.connect();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        for (String path : testFilePath) {
            InputStream inputStream = sftp.get(path);
            String fileName = path.substring(path.lastIndexOf("/"));
            try (ZipOutputStream zipOutputStream = new ZipOutputStream(bos)){
                ZipEntry zipEntry = new ZipEntry(fileName);
                zipOutputStream.putNextEntry(zipEntry);
                IOUtils.copy(inputStream,zipOutputStream);
                zipOutputStream.closeEntry();
                inputStream.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(bos.size());
    }
}
