package com.shucha.deveiface.biz.test;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author tqf
* @Description
* @Version 1.0
* @since 2024-02-19 11:20
*/
public class t2 {
public static void main(String[] args) {
List<String> list = new ArrayList< >();
list.add("D:\\20210213\\1.xls");
list.add("D:\\20210213\\2.xls");
list.add("D:\\20210213\\3.xls");
list.add("D:\\20210213\\2.jpeg");
list.add("D:\\20210213\\2.txt");
list.add("D:\\20210213\\2.docx");
fileToZip(list, "D:\\20210213\\123.zip");
}
/**
* 多个文件打包成zip文件包
* @param list 需要打包的文件路径列表
* @param zipPath 生成的文件压缩包文件存放路径
*/
public static void fileToZip ( List<String> list , String zipPath){
try(
FileOutputStream out = new FileOutputStream(zipPath);
ZipOutputStream zipOut = new ZipOutputStream (out);
){
for (String filePath : list) {
FileInputStream in = new FileInputStream (filePath);
// 创建ZipEntry 对象 设置名称
File file = new File ( filePath );
ZipEntry zipEntry = new ZipEntry ( file . getName ());
zipOut.putNextEntry(zipEntry );
// 读取源文件的内容写入 zip
byte [] bytes = new byte [1024];
int length ;
while (( length = in . read ( bytes ))>=0) {
zipOut.write(bytes, 0, length);
}
}
zipOut.closeEntry ();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
多个文件打包生成压缩包
最新推荐文章于 2024-07-09 16:46:46 发布
![](https://img-home.csdnimg.cn/images/20240709112858.png)