压缩包工具类

压缩包工具类(下载)


import com.enlistboot.common.core.FileResult;
import org.springframework.http.HttpHeaders;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    /**
     * 压缩包下载
     *
     * @param response
     * @param fileName
     * @param list
     */
    public static void downloadZip(HttpServletResponse response, String fileName, List<FileResult> list) {

        try {
            BufferedInputStream bis = null;
            java.io.File file = null;
            ByteArrayOutputStream output = new ByteArrayOutputStream();//ByteArray临时存储流

            ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
            InputStream fis = null;


            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader(Content-Disposition, "attachment;filename=\"" + URLEncoder.encode(fileName + ".zip", "UTF-8") + "\"");


            Map<String, String> map = new HashMap<>();


            for (FileResult studentInfoVo : list) {
//                FileInputStream fis = new FileInputStream(new File(studentInfoVo.getKszp()));

                if (ObjectUtils.isEmpty(map.get(studentInfoVo.getFolderName()))) {
                    String path = studentInfoVo.getUrl();
//                    String path = "https://img1.baidu.com/it/u=3539595421,754041626&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500";
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);

                    fis = connection.getInputStream();

                    // 向zip输出流添加该文件的zip实体
                    if (ObjectUtils.isEmpty(map.get(studentInfoVo.getFolderName()))) {
                    zipOut.putNextEntry(new ZipEntry(fileName + "/" + studentInfoVo.getFolderName()));
                    }

                    // 使用缓冲流提高效率
                    bis = new BufferedInputStream(fis);
                    // 缓冲字节数组
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    // 将文件数据写入zip输出流
                    while ((len = bis.read(buffer)) != -1) {
                        output.reset();
                        output.write(buffer, 0, len);
                        // 使用OutputStream将缓冲区的数据输出到客户端浏览器
                        zipOut.write(output.toByteArray());
                    }
                    map.put(studentInfoVo.getFolderName(),studentInfoVo.getFolderName());

                    zipOut.flush();
                    zipOut.closeEntry();
                    bis.close();
                    output.close();

                }

            }
            fis.close();

            zipOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java,文件压缩包)