基于Java向zip压缩包追加文件
作者:gdjlc 时间:2023-10-11 17:18:52
这篇文章主要介绍了基于Java向zip压缩包追加文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
有个需求,从某个接口下载的一个zip压缩包,往里面添加一个说明文件。搜索了一下,没有找到往zip直接添加文件的方法,最终解决方法是先解压、再压缩。
具体过程如下:
1、一个zip文件的压缩和解压工具类
pom.xml加入依赖包,如下:
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.7</version>
</dependency>
工具类代码:
package com.example.demo;
import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipException;
import org.apache.tools.zip.*;
public class ZipUtil {
private static int BUFFERSIZE = 1024;
/**
* 压缩
*
* @param paths
* @param fileName
*/
public static void zip(List<String> paths, String fileName) {
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(fileName));
for (String filePath : paths) {
// 递归压缩文件
File file = new File(filePath);
String relativePath = file.getName();
if (file.isDirectory()) {
relativePath += File.separator;
}
zipFile(file, relativePath, zos);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void zipFile(File file, String relativePath, ZipOutputStream zos) {
InputStream is = null;
try {
if (!file.isDirectory()) {
ZipEntry zp = new ZipEntry(relativePath);
zos.putNextEntry(zp);
is = new FileInputStream(file);
byte[] buffer = new byte[BUFFERSIZE];
int length = 0;
while ((length = is.read(buffer)) >= 0) {
zos.write(buffer, 0, length);
}
zos.setEncoding("gbk");//解决文件名中文乱码
zos.flush();
zos.closeEntry();
} else {
String tempPath = null;
for (File f : file.listFiles()) {
tempPath = relativePath + f.getName();
if (f.isDirectory()) {
tempPath += File.separator;
}
zipFile(f, tempPath, zos);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 解压缩
*
* @param fileName
* @param path
*/
public static List<String> unzip(String fileName, String path) {
FileOutputStream fos = null;
InputStream is = null;
List<String> filePaths = new ArrayList<String>();
try {
ZipFile zf = new ZipFile(new File(fileName));
Enumeration en = zf.getEntries();
while (en.hasMoreElements()) {
ZipEntry zn = (ZipEntry) en.nextElement();
if (!zn.isDirectory()) {
is = zf.getInputStream(zn);
File f = new File(path + zn.getName());
File file = f.getParentFile();
file.mkdirs();
fos = new FileOutputStream(path + zn.getName());
int len = 0;
byte bufer[] = new byte[BUFFERSIZE];
while (-1 != (len = is.read(bufer))) {
fos.write(bufer, 0, len);
}
fos.close();
filePaths.add(path + zn.getName());
}
}
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
if (null != fos) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return filePaths;
}
}
2、测试
有如下目录结构:
D:\测试\文档.zip
D:\测试\说明.pdf
把“说明.pdf”添加到“文档.zip”里面,生成一个新压缩包“文档(新).zip”。
package com.example.demo;
import java.io.File;
import java.util.List;
public class ZipUtilTest {
public static void main(String[] args) {
//解压
List<String> files = ZipUtil.unzip("D:/测试/文档.zip", "D:/测试/");
//集合添加文件
files.add("D:/测试/说明.pdf");
//压缩
ZipUtil.zip(files,"D:/测试/文档(新).zip");
//保留说明.pdf
files.remove(files.size()-1);
//删除上面解压出来的文件
for(String f : files){
File file = new File(f);
if(file.exists()){
file.delete();
}
}
}
}
来源:https://www.cnblogs.com/gdjlc/p/12019833.html
标签:Java,zip,压缩包
0
投稿
猜你喜欢
Java SWT中常见弹出框实例总结
2023-08-22 00:42:22
Java设计通用的返回数据格式过程讲解
2023-11-09 00:16:40
Android使用 Retrofit 2.X 上传多文件和多表单示例
2023-08-06 03:48:25
Spring Security实现两周内自动登录"记住我"功能
2021-12-15 17:59:22
C#生成随机数实例
2023-09-17 23:28:28
JavaWeb中使用JavaMail实现发送邮件功能实例详解
2023-01-07 13:54:37
最常用和最难用的Android控件ListView
2023-06-13 07:58:11
Android仿简书搜索框效果的示例代码
2023-06-18 16:02:58
Spring Boot 文件上传与下载的示例代码
2021-08-30 02:04:53
spring依赖注入知识点分享
2023-11-26 15:32:32
关于Maven的使用,这些你都真的了解么
2022-01-02 14:19:08
Java如何把int类型转换成byte
2023-03-13 11:12:39
idea maven pom不自动更新的解决方法
2021-11-19 14:56:46
spring mvc实现登录账号单浏览器登录
2022-06-28 22:29:47
c# winform取消右上角关闭按钮的实现方法
2023-05-05 18:56:28
SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI
2022-07-08 07:40:23
Android编程实现获取新浪天气预报数据的方法
2022-08-15 22:14:44
Android深入浅出之Binder机制
2021-07-06 01:47:21
Java中静态类型检查是如何进行的实例思路详解
2022-01-01 16:08:30
java微信公众号支付开发之现金红包
2023-09-01 17:28:38