JAVA中将两个byte[]文件数据添加到压缩包中进行下载、压缩包设置密码

 

 

  String zipFileName="压缩包名.zip";

        // 设置响应头,告诉浏览器返回的是否是一个文件流
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + zipFileName);

        // 创建一个字节数组输出流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // 创建一个压缩输出流
        ZipOutputStream zos = new ZipOutputStream(bos);
        try {
            // 将byte数组文件逐个添加到压缩包中
        
            zos.putNextEntry(new ZipEntry("文件名"));
            zos.write(文件的byte[]);
            zos.closeEntry();


            zos.putNextEntry(new ZipEntry("文件名"));
            zos.write(文件的byte[]);
            zos.closeEntry();

            // 完成压缩并将压缩包写入到输出流中
            zos.finish();

            // 将输出流中的内容写入响应流中
            response.getOutputStream().write(bos.toByteArray());
        } catch (IOException e) {
           log.info(">>>>>文件下载异常:{}",e);
        }

 

 

 压缩包加密设置密码

依赖

  <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.11.5</version>
        </dependency>

 

 

 

工具类

import com.ai.gaimops.utils.core.BusinessException;
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.AesKeyStrength;
import net.lingala.zip4j.model.enums.EncryptionMethod;

import java.io.InputStream;
import java.io.OutputStream;

public class ZipUtil {

    /**
     * 加密压缩
     *
     * @param output        输出
     * @param input         被压缩文件
     * @param fileNameInZip 压缩包中文件名称
     * @param password      压缩密码
     */
    public static void zip(OutputStream output, InputStream input, String fileNameInZip, String password) {
        try {
            ZipParameters zipParameters = new ZipParameters();
            zipParameters.setEncryptFiles(true);
            zipParameters.setEncryptionMethod(EncryptionMethod.AES);
            zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
            zipParameters.setFileNameInZip(fileNameInZip);
            byte[] buff = new byte[4096];
            int readLen;
            try (ZipOutputStream zos = new ZipOutputStream(output, password.toCharArray())) {
                zos.putNextEntry(zipParameters);
                try (InputStream inputStream = input) {
                    while ((readLen = inputStream.read(buff)) != -1) {
                        zos.write(buff, 0, readLen);
                    }
                }
                zos.closeEntry();
            }
        } catch (Exception e) {
            throw new BusinessException("压缩失败", e);
        }
    }
}

 

调用示例

 /**
     * 压缩返回,并且发送邮件
     * @param fileName
     * @param workbook
     * @throws IOException
     */
    protected String zipResponse(String fileName, Workbook workbook) throws IOException {
        String zipName = fileName + ".zip";
        HttpServletResponse servletResponse = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        servletResponse.setContentType("application/vnd.ms-excel;charset=UTF-8");
        servletResponse.setHeader("Content-Disposition", "attachment;filename=" +new String(URLEncodeUtil.encode(zipName).getBytes(), "ISO8859-1"));
        servletResponse.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        servletResponse.flushBuffer();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);

        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        String password = RandomUtil.randomString(6);
        ZipUtil.zip(servletResponse.getOutputStream(), inputStream, fileName + ".xlsx", password);
        return password;
    }

 

posted @ 2023-06-19 15:41  yvioo  阅读(788)  评论(0)    收藏  举报