JAVA中多个生成多个文件流并打包成zip的方式,全部流操作完成

首先,项目中需要引入jar包

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.12</version>
</dependency>

 我们用多个excel文件流生成zip包做示范:

//1:获取excel的文件list

List<Workbook> workbookList = XXXXService.getBillWorkbookList(xxxxList);

//2:吧response的流给到ZipArchiveOutputStream并创建该对象
                ZipArchiveOutputStream zous = new ZipArchiveOutputStream(outputStream);
                zous.setUseZip64(Zip64Mode.AsNeeded);

//遍历文件list
                for (Workbook workbook : workbookList) {

                    //给文件名
                    String fileName = UUID.randomUUID() + ".xlsx";

                    //下面三行是吧excel的文件以流的形式转为byte[]
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    workbook.write(baos);
                    byte[] bytes = baos.toByteArray();
                    ArchiveEntry entry = new ZipArchiveEntry(fileName);
                    zous.putArchiveEntry(entry);
                    zous.write(bytes);
                    zous.closeArchiveEntry();
                    if (baos != null) {
                        baos.close();
                    }
                }
                zous.close();
            }

这样就可以完成该操作了

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将多个.docx文件打包为一个.zip压缩包,你可以使用Javajava.util.zip包和java.io包。以下是一个示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipMultipleDocxFiles { public static void main(String[] args) { List<String> docxFiles = new ArrayList<>(); docxFiles.add("path/to/docx/file1.docx"); docxFiles.add("path/to/docx/file2.docx"); docxFiles.add("path/to/docx/file3.docx"); String zipFilePath = "path/to/zip/file.zip"; try { // 创建输出 FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); // 逐个文件将docx文件添加到ZipOutputStream for (String docxFilePath : docxFiles) { File docxFile = new File(docxFilePath); FileInputStream fis = new FileInputStream(docxFile); // 创建ZipEntry并添加到ZipOutputStream ZipEntry zipEntry = new ZipEntry(docxFile.getName()); zos.putNextEntry(zipEntry); // 将docx文件内容写入ZipOutputStream byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { zos.write(buffer, 0, bytesRead); } // 关闭当前ZipEntry的输入 fis.close(); zos.closeEntry(); } // 关闭ZipOutputStream zos.close(); System.out.println("打包功!"); } catch (IOException e) { System.out.println("打包失败:" + e.getMessage()); } } } ``` 请将`docxFiles`列表文件路径替换为你实际的.docx文件路径。运行以上代码后,将会在指定的路径生成一个名为"file.zip"的压缩文件,其包含了你指定的多个.docx文件。 此代码示例会逐个将.docx文件添加到压缩文件,并在压缩文件创建与每个.docx文件名称相对应的ZipEntry。请确保文件路径正确,并注意文件名在压缩文件的唯一性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值