package com.face.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
public class ZipUtils {
    /**
     * 压缩文件
     *
     * @param zipOutputSteam
     * @throws Exception
     */
    public static void zipFile(File file, String fileName, ZipOutputStream zipOutputSteam) throws Exception {
        try (FileInputStream inputStream = new FileInputStream(file)) {
            ZipEntry zipEntry = new ZipEntry(fileName);
            zipOutputSteam.putNextEntry(zipEntry);
            int len;
            byte[] buffer = new byte[4096];
            while ((len = inputStream.read(buffer)) != -1) {
                zipOutputSteam.write(buffer, 0, len);
            }
            zipOutputSteam.closeEntry();
        }
    }

    public static void zipBlank(ZipOutputStream zipOutputStream) throws IOException {
        ZipEntry zipEntry = new ZipEntry("empty");
        zipOutputStream.putNextEntry(zipEntry);
        zipOutputStream.closeEntry();
    }
}