java实现一次性压缩多个文件到zip中的方法示例
作者:yqwang75457 时间:2021-11-16 07:24:47
本文实例讲述了java实现一次性压缩多个文件到zip中的方法。分享给大家供大家参考,具体如下:
1.需要引入包:
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;
import org.springframework.util.StringUtils;
2.代码
/**
* @Title: compress
* @Description: TODO
* @param filePaths 需要压缩的文件地址列表(绝对路径)
* @param zipFilePath 需要压缩到哪个zip文件(无需创建这样一个zip,只需要指定一个全路径)
* @param keepDirStructure 压缩后目录是否保持原目录结构
* @throws IOException
* @return int 压缩成功的文件个数
*/
public static int compress(List<String> filePaths, String zipFilePath,Boolean keepDirStructure) throws IOException{
byte[] buf = new byte[1024];
File zipFile = new File(zipFilePath);
//zip文件不存在,则创建文件,用于压缩
if(!zipFile.exists())
zipFile.createNewFile();
int fileCount = 0;//记录压缩了几个文件?
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i = 0; i < filePaths.size(); i++){
String relativePath = filePaths.get(i);
if(StringUtils.isEmpty(relativePath)){
continue;
}
File sourceFile = new File(relativePath);//绝对路径找到file
if(sourceFile == null || !sourceFile.exists()){
continue;
}
FileInputStream fis = new FileInputStream(sourceFile);
if(keepDirStructure!=null && keepDirStructure){
//保持目录结构
zos.putNextEntry(new ZipEntry(relativePath));
}else{
//直接放到压缩包的根目录
zos.putNextEntry(new ZipEntry(sourceFile.getName()));
}
//System.out.println("压缩当前文件:"+sourceFile.getName());
int len;
while((len = fis.read(buf)) > 0){
zos.write(buf, 0, len);
}
zos.closeEntry();
fis.close();
fileCount++;
}
zos.close();
//System.out.println("压缩完成");
} catch (Exception e) {
e.printStackTrace();
}
return fileCount;
}
3.测试
public static void main(String[] args) throws IOException {
List<String> sourceFilePaths = new ArrayList<String>();
sourceFilePaths.add("d:/test/C08065.jpg");
sourceFilePaths.add("d:/test/新建文件夹/C08984.jpg");
sourceFilePaths.add("d:/test/找不到我.jpg");//试一个找不到的文件
//指定打包到哪个zip(绝对路径)
String zipTempFilePath = "D:/test/test.zip";
//调用压缩
int s = compress(sourceFilePaths, zipTempFilePath,false);
System.out.println("成功压缩"+s+"个文件");
}
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/yqwang75457/article/details/94023451
标签:java,压缩,zip
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用C#操作WMI指南
2022-05-07 18:02:42
C#实现读取指定盘符硬盘序列号的方法
2023-05-23 15:06:39
java swing实现QQ账号密码输入框
2023-01-13 06:26:52
![](https://img.aspxhome.com/file/2023/2/71592_0s.png)
SpringBoot 如何从容器中获取对象
2023-12-06 08:20:40
![](https://img.aspxhome.com/file/2023/5/75775_0s.png)
java实现简单年龄计算器
2022-01-28 02:23:34
![](https://img.aspxhome.com/file/2023/1/63041_0s.png)
IntelliJ IDEA(2020.2)的下载、安装步骤详细教程
2023-11-25 07:10:16
![](https://img.aspxhome.com/file/2023/0/59900_0s.png)
C#利用缓存分块读写大文件
2022-10-20 11:18:22
C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法
2023-06-30 12:55:22
C#游戏开发之实现贪吃蛇游戏
2023-01-28 01:48:48
![](https://img.aspxhome.com/file/2023/9/83309_0s.png)
Linux中Java开发常用软件安装方法总结
2022-03-11 16:21:03
![](https://img.aspxhome.com/file/2023/2/85912_0s.png)
mybatis如何批量添加一对多中间表
2021-08-26 03:16:41
![](https://img.aspxhome.com/file/2023/7/72337_0s.png)
Java开发学习 Eclipse项目有红感叹号解决之道
2022-10-22 15:29:27
编写android拨打电话apk应用实例代码
2021-08-25 11:09:12
c# Selenium爬取数据时防止webdriver封爬虫的方法
2023-06-24 07:50:51
![](https://img.aspxhome.com/file/2023/7/59117_0s.png)
C#与java TCP通道加密通信实例
2023-12-03 15:44:29
Android提醒微技巧你真的了解Dialog、Toast和Snackbar吗
2023-03-08 14:15:44
![](https://img.aspxhome.com/file/2023/0/103890_0s.jpg)
Java 根据网址查询DNS/IP地址的方法
2023-06-21 15:31:54
Android拼图游戏 玩转从基础到应用手势变化
2021-08-24 02:41:59
![](https://img.aspxhome.com/file/2023/7/95967_0s.gif)
springboot整合RabbitMQ 中的 TTL实例代码
2022-04-18 03:12:34
![](https://img.aspxhome.com/file/2023/3/103983_0s.png)
利用Java自写一个生成ID的工具类
2023-04-24 04:10:08