Java 将多个文件打包为zip压缩包并提供给前端下载

一、文件打包为zip

public void zip(List<File> fileList,String zipFileName) {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		ZipOutputStream zipOutputStream = null;
		BufferedInputStream bufferInputStream = null;
		try {
			// zipFileName为压缩文件的名称(xx.zip),首先在某个目录下(C:/temp/路径可以根据自己的需求进行修改)创建一个.zip结尾的文件
			fileOutputStream = new FileOutputStream(new File("C:/temp/" + zipFileName));
			zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));
			// 创建读写缓冲区
			byte[] bufs = new byte[1024 * 10];

			for (File file : fileList) {
				// 创建ZIP实体,并添加进压缩包
				ZipEntry zipEntry = new ZipEntry(file.getName());
				zipOutputStream.putNextEntry(zipEntry);

				// 读取待压缩的文件并写进压缩包里
				fileInputStream = new FileInputStream(file);
				bufferInputStream = new BufferedInputStream(fileInputStream, 1024 * 10);
				int read = 0;
				while ((read = bufferInputStream.read(bufs, 0, 1024 * 10)) != -1) {
					zipOutputStream.write(bufs, 0, read);
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bufferInputStream != null) {
					bufferInputStream.close();
				}
				if (zipOutputStream != null) {
					zipOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

二、下载zip包

	public void downloadZip(HttpServletResponse response, String zipFileName) {
		// zipName为上一步文件打包zip时传入的zipName
		File zipFile = new File("C:/temp/" + zipFileName);
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename=" + zipFileName);

		FileInputStream fileInputStream = null;
		OutputStream outputStream = null;
		try {
			outputStream = response.getOutputStream();
			fileInputStream = new FileInputStream(zipFile);
			byte[] bufs = new byte[1024 * 10];
			int read = 0;
			while ((read = fileInputStream.read(bufs, 0, 1024 * 10)) != -1) {
				outputStream.write(bufs, 0, read);
			}
			fileInputStream.close();
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				// 删除压缩包
				File file = new File("C:/temp/" + zipFileName);
				file.delete();
                
				if (fileInputStream != null) {
					fileInputStream.close();
				}
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

三、下载接口

	@GetMapping("/downloadZip")
	@ApiOperation("下载zip")
	public void downloadZip(HttpServletResponse response) {
		downloadService.downloadZip(response);
	}
要将多个.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、付费专栏及课程。

余额充值