1.pom.xml
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.1</version>
</dependency>
2.具体工具类实现
package com.lancet.util;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
public class ZipUtil {
private static final int buffer = 2048;
/**
* 解压Zip文件
*
* @param path
* 文件目录
*/
public static void unZip(String path) {
int count = -1;
String savepath = "";
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
savepath = path.substring(0, path.lastIndexOf(".")) + File.separator; // 保存解压文件目录
new File(savepath).mkdir(); // 创建保存目录
ZipFile zipFile = null;
try {
zipFile = new ZipFile(path, "GBK"); // 解决中文乱码问题
Enumeration<?> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
byte buf[] = new byte[buffer];
ZipEntry entry = (ZipEntry) entries.nextElement();
String filename = entry.getName();
boolean ismkdir = false;
if (filename.lastIndexOf("/") != -1) { // 检查此文件是否带有文件夹
ismkdir = true;
}
filename = savepath + filename;
if (entry.isDirectory()) { // 如果是文件夹先创建
file = new File(filename);
file.mkdirs();
continue;
}
file = new File(filename);
if (!file.exists()) { // 如果是目录先创建
if (ismkdir) {
new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs(); // 目录先创建
}
}
file.createNewFile(); // 创建文件
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer);
while ((count = is.read(buf)) > -1) {
bos.write(buf, 0, count);
}
bos.flush();
bos.close();
fos.close();
is.close();
}
zipFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (is != null) {
is.close();
}
if (zipFile != null) {
zipFile.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
fileToZip("d:/aaa","d:/aaa","asd");
// StringUtil.deleteFolder("d:/aaa");
System.out.println(StringUtil.getMd5ByFile(new File("d:/aaa/asd.zip")));
}
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
*
* @param sourceFilePath
* :待压缩的文件路径
* @param zipFilePath
* :压缩后存放路径
* @param fileName
* :压缩后文件的名称
* @return
*/
public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
boolean flag = false;
File sourceFile = new File(sourceFilePath);
BufferedInputStream bis = null;
ZipOutputStream zos = null;
if (sourceFile.exists() == false) {
System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
} else {
try {
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
if (zipFile.exists()) {
System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
} else {
File[] sourceFiles = sourceFile.listFiles();
if (null == sourceFiles || sourceFiles.length < 1) {
System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
} else {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
byte[] bufs = new byte[1024 * 10];
for (int i = 0; i < sourceFiles.length; i++) {
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zipEntry.setTime(1);
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
bis = new BufferedInputStream(new FileInputStream(sourceFiles[i]), 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
bis.close();
bis = null;
}
flag = true;
zos.finish();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (zos != null) {
try {
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
zos = null;
}
}
}
return flag;
}
/**
* 压缩目录下的文件
* @param filePath 要压缩的目录路径
* @param zipPath 生成的压缩包存放路径
* @throws FileNotFoundException
*/
public static void zipMutilFile(String filePath, String zipPath) throws FileNotFoundException {
File file = new File(filePath);
if (!file.isDirectory()) {
throw new FileNotFoundException("This file is not a directory.");
}
File zipFile = new File(zipPath);
InputStream inputStream = null;
try {
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
File[] files = file.listFiles();
for (File file1 : files) {
inputStream = new FileInputStream(file1);
zipOutputStream.putNextEntry(new ZipEntry(file.getName() + File.separator + file1.getName()));
int temp = 0;
while ((temp = inputStream.read()) != -1) {
zipOutputStream.write(temp);
}
inputStream.close();
}
zipOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}