package com.post;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.generator.util.VelocityUtils;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipTest {
    public static void main(String[] args) throws Exception {

        // 压缩文件的输出路径
        String zipFilePath = "C:\\gnhz\\uploadPath\\download\\批量导出岗位检查表-" + DateUtils.dateTimeNow() + ".zip";

        // 要压缩的文件路径
        File filePath = new File("C:\\gnhz\\uploadPath\\download\\EHR中不存在的岗检系统人员_20240312112506-543164.xlsx");




        // 创建文件输入流
        FileInputStream inputStream = new FileInputStream(filePath);
        // 创建ZipOutputStream输出流
        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath));

        // 创建ZipEntry
        ZipEntry zipEntry = new ZipEntry(filePath.getName());
        // 添加ZipEntry到ZipOutputStream中
        zipOutputStream.putNextEntry(zipEntry);

        // 读取文件内容并写入到ZipOutputStream中
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buffer)) > 0) {
            zipOutputStream.write(buffer, 0, length);
        }

        inputStream.close();
        // 关闭当前ZipEntry
        zipOutputStream.closeEntry();
        zipOutputStream.flush();
        zipOutputStream.close();

        System.out.println("文件成功压缩到111: " + zipFilePath);


//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        ZipOutputStream zip = new ZipOutputStream(outputStream);
//        //填充数据
//
//
//        for (int i = 0; i < 1; i++) {
//            // 添加到zip
//            zip.putNextEntry(new ZipEntry("C:\\gnhz\\uploadPath\\download\\1.txt"));
//            IOUtils.write(sw.toString(), zip, Constants.UTF8);
//            IOUtils.closeQuietly(sw);
//            zip.flush();
//            zip.closeEntry();
//        }
//
//
//        //
//        IOUtils.closeQuietly(zip);
//        byte[] data = outputStream.toByteArray();
//
//
//        IOUtils.closeQuietly(zip);
//
//        //写文件
//        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\gnhz\\uploadPath\\download\\批量导出岗位检查表-" + DateUtils.dateTimeNow() + ".zip"));
//        IOUtils.write(data, fileOutputStream);
    }
}
