package com.cirle.scientific.utils;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class FileUtils {


    /**
     * 1 多文件下载
     *
     * @param response     HttpServletResponse
     * @param files[]      待下载文件
     * @param downloadfile 下载文件文件名+文件类型(a.zip)
     * @throws Exception
     * @date 2019年3月29日11:31:35
     */
    public void multFileDownload(HttpServletResponse response, String[] files, String downloadfile) throws Exception {
        response.setContentType("multipart/form-data");//1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setHeader("Content-Disposition", "attachment;fileName=" + downloadfile);
        FileInputStream instream = null;
        try {
            ZipOutputStream zipstream = new ZipOutputStream(response.getOutputStream());
            for (String file : files) {
                if (!new File(file).exists()) {
                    continue;
                }
                instream = new FileInputStream(file);
                ZipEntry entry = new ZipEntry(file);
                zipstream.putNextEntry(entry);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = instream.read(buffer)) != -1) {
                    zipstream.write(buffer, 0, len);
                }
                instream.close();
                zipstream.closeEntry();
                zipstream.flush();
            }
            zipstream.finish();
            zipstream.close();
        } catch (IOException e) {
            new RuntimeException(e.getMessage());
        }
    }

    /**
     * 2 多文件下载
     *
     * @param response     HttpServletResponse
     * @param files[]      待下载文件
     * @param downloadfile 下载文件
     * @param path         待下载文件路径
     * @param tempfile     待下载临时文件
     * @throws Exception
     * @date 2019年3月29日11:31:35
     */
    public void multFileDownload(HttpServletResponse response, String[] files, String path, String downloadfile) throws Exception {
        response.setContentType("multipart/form-data");//1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setHeader("Content-Disposition", "attachment;fileName=" + downloadfile);
        FileInputStream instream = null;
        try {
            ZipOutputStream zipstream = new ZipOutputStream(response.getOutputStream());
            for (String file : files) {
                if (!new File(path + file).exists()) {
                    continue;
                }
                instream = new FileInputStream(path + file);
                ZipEntry entry = new ZipEntry(path + file);
                zipstream.putNextEntry(entry);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = instream.read(buffer)) != -1) {
                    zipstream.write(buffer, 0, len);
                }
                instream.close();
                zipstream.closeEntry();
                zipstream.flush();
            }
            zipstream.finish();
            zipstream.close();
        } catch (IOException e) {
            new RuntimeException(e.getMessage());
        }
    }

    /**
     * 1 单文件下载
     *
     * @param response     HttpServletResponse
     * @param filepath     待下载文件路径
     * @param downloadfile 下载文件
     * @date 2019年3月29日11:31:35
     */
    public void singleFileDownload(HttpServletResponse response, String filepath, String downloadfile) {
        response.setContentType("multipart/form-data");//1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setHeader("Content-Disposition", "attachment;fileName=" + downloadfile);
        ServletOutputStream out;
        File file = new File(filepath);
        if (!file.exists()) {
            new RuntimeException("文件不存在");
        }
        try {
            FileInputStream inputStream = new FileInputStream(file);
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            inputStream.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            new RuntimeException(e.getMessage());
        }
    }

    /**
     * 2 单文件下载
     *
     * @param response HttpServletResponse
     * @param filepath 待下载文件路径
     * @param filename 下载文件名称
     * @param filetype 下载文件类型
     * @date 2019年3月29日11:31:35
     */
    public void singleFileDownload(HttpServletResponse response, String filepath, String filename, String filetype) {
        response.setContentType("multipart/form-data");//1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setHeader("Content-Disposition", "attachment;fileName=" + filename + filetype);
        ServletOutputStream out;
        File file = new File(filepath);
        if (!file.exists()) {
            new RuntimeException("文件不存在");
        }
        try {
            FileInputStream inputStream = new FileInputStream(file);
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            inputStream.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            new RuntimeException(e.getMessage());
        }
    }

    /**
     * 1 多文件 压缩成 ZIP
     *
     * @param files         文件名字
     * @param path          文件路径
     * @param loaclfilepath 临时文件全路径
     * @date 2019年3月29日11:31:35
     */
    public void toZip(String[] files, String path, String loaclfilepath) throws Exception {
        FileInputStream instream = null;
        ZipOutputStream zipstream = new ZipOutputStream(new FileOutputStream(new File(loaclfilepath)));
        for (String file : files) {
            if (!new File(path + file).exists()) {
                continue;
            }
            instream = new FileInputStream(path + file);
            ZipEntry entry = new ZipEntry(path + file);
            zipstream.putNextEntry(entry);
            byte[] buffer = new byte[1024];
            while ((instream.read(buffer)) != -1) {
                zipstream.write(buffer, 0, buffer.length);
            }
            zipstream.closeEntry();
            zipstream.flush();
        }
        zipstream.finish();
        zipstream.close();
    }

    /**
     * 2 多文件 压缩成 ZIP
     *
     * @param files         文件名字
     * @param path          文件路径
     * @param loaclfilepath 压缩文件全路径
     * @date 2019年3月29日11:31:35
     */
    public void toZip(List<String> files, String path, String loaclfilepath) throws Exception {
        FileInputStream instream = null;
        ZipOutputStream zipstream = new ZipOutputStream(new FileOutputStream(new File(loaclfilepath)));
        for (String file : files) {
            if (!new File(path + file).exists()) {
                continue;
            }
            instream = new FileInputStream(path + file);
            ZipEntry entry = new ZipEntry(path + file);
            zipstream.putNextEntry(entry);
            byte[] buffer = new byte[1024];
            while ((instream.read(buffer)) != -1) {
                zipstream.write(buffer, 0, buffer.length);
            }
            zipstream.closeEntry();
            zipstream.flush();
        }
        zipstream.finish();
        zipstream.close();
    }


}
