您的位置:首页 > 编程语言 > Java开发

基于Java向zip压缩包追加文件

2019-12-12 12:10 92 查看

这篇文章主要介绍了基于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();
}
}
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java zip 压缩包