1、pom依赖
<!-- zip打包工具-->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>
2、utils工具类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
/**
* 压缩一个文件或者目录
*
* @param zipFileName 压缩后的文件名(绝对路径):
* 如E:\app_data\\upload\temp\batchDownload\A2023001_检查.zip
* @param zipFilePath 需要被压缩的文件路径(绝对路径):
* 如E:\app_data\\upload\temp\batchDownload\A2023001_检查
* @throws Exception
*/
public static void zip(String zipFileName, String zipFilePath) throws Exception {
zip(zipFileName, new File(zipFilePath));
}
/**
* @param zipFileName 压缩后的文件名及路径
* @param zipFilePath 要被压缩的文件的输入流
* @throws Exception
*/
public static void zip(String zipFileName, File zipFilePath) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, zipFilePath, "");
System.out.println("zip done");
out.close();
}
/**
* 用于压缩整个目录或者单个文件
*
* @param out 源文件的输出流
* @param f 目标压缩文件的输入流
* @param base a
* @throws Exception
*/
private static void zip(ZipOutputStream out, File f, String base) throws Exception {
System.out.println("Zipping " + f.getName());
if (f.isDirectory()) {
File[] fl = f.listFiles();
//out.putNextEntry(new ZipEntry(base+"/"));
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + f.getName() + "/"));
for (int i = 0; i < fl.length; i++) {
//zip(out,fl[i],base);
zip(out, fl[i], base + f.getName() + "/");
}
} else {
//base=base.length()==0?"":base+"/";
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + f.getName()));
FileInputStream in = new FileInputStream(f);
int len;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
}
}
/**
* 解压缩
*
* @param zipFileName 压缩文件
* @param outputDirectory 目标路径
* @throws Exception
*/
public static void unzip(String zipFileName, String outputDirectory) throws Exception {
unzip(zipFileName, outputDirectory, Charset.forName("GBK"));
}
/**
* 解压缩
*
* @param zipFile 压缩文件
* @param outputDirectory 目标路径
* @throws Exception
*/
public static void unzip(File zipFile, String outputDirectory) throws Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry z;
while ((z = in.getNextEntry()) != null) {
System.out.println("unziping " + z.getName());
if (z.isDirectory()) {
String name = z.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("mkdir " + outputDirectory + File.separator + name);
} else {
File f = new File(outputDirectory + File.separator + z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
out.close();
}
}
in.close();
}
/**
* 解压缩
*
* @param zipFileName 压缩文件
* @param outputDirectory 目标路径
* @throws Exception
*/
public static void unzip(String zipFileName, String outputDirectory, Charset charset) throws Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName), charset);
ZipEntry z;
while ((z = in.getNextEntry()) != null) {
System.out.println("unziping " + z.getName());
if (z.isDirectory()) {
String name = z.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("mkdir " + outputDirectory + File.separator + name);
} else {
File f = new File(outputDirectory + File.separator + z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
out.close();
}
}
in.close();
}
//测试压缩文件
public static void main(String[] args) throws Exception {
String zipFileName = "E:\\test\\aa.zip";
String zipFilePath = "E:\\test\\aa";
zip(zipFileName, zipFilePath);
}
}