依赖第三方jar包:ant-1.7.1.jar
/**
* @Title: ZipUtils.java
* @Package com.sz.mt.test.zip
* @Description: TODO(ZIP压缩及解压工具类)
* @author: LokChow
* @version V1.0
*/
package com.sz.mt.test.zip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* @ClassName: ZipUtils
* @Description: TODO(ZIP实现文件的压缩和解压缩工具类)
* @author LokChow
* @date 2018-11-27
*
*/
public class ZipUtils {
private static final int BUFFER_SIZE = 4 * 1024;
/**
* @Title: unzip
* @Description: TODO(解压zip格式压缩包)
* @param @param sourceZip 待解压文件路径
* @param @param destDir 解压后的路径
* @param @return 参数
* @return boolean 返回类型
* @throws
*/
private static boolean unzip(String sourceZip, String destDir) {
try {
Project project = new Project();
Expand expand = new Expand();
expand.setProject(project);
expand.setSrc(new File(sourceZip));
expand.setOverwrite(false);
expand.setDest(new File(destDir));
/*
* ant下的zip工具默认压缩编码为UTF-8编码,
* 而winRAR软件压缩是用的windows默认的GBK或者GB2312编码
* 所以解压缩时要制定编码格式
*/
expand.setEncoding("gbk");
expand.execute();
} catch (BuildException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 递归压缩方法
* @param sourceFile 源文件
* @param zos zip输出流
* @param name 压缩后的名称
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @throws Exception
*/
private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean KeepDirStructure) throws Exception{
if (!sourceFile.exists()) return;
FileInputStream fis = null;
byte buf[] = new byte[BUFFER_SIZE];
if (sourceFile.isFile()) {
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(new ZipEntry(name));
// copy文件到zip输出流中
int len;
fis = new FileInputStream(sourceFile);
while((len = fis.read(buf)) != -1){
zos.write(buf,0,len);//copy并写到压缩文件中
}
// 解压完毕
zos.closeEntry();
fis.close();
}else{
File Listfiles[] = sourceFile.listFiles();
//没有文件
if (Listfiles == null || Listfiles.length == 0) {
// 需要保留原来的文件结构时,需要对空文件夹进行处理
if (KeepDirStructure) {
// 空文件夹的处理
zos.putNextEntry(new ZipEntry(name+"/"));
// 没有文件,不需要文件的copy
zos.closeEntry();
}
}else{
for (File file : Listfiles) {
// 判断是否需要保留原来的文件结构
if (KeepDirStructure) {
// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress(file, zos, name+"/"+file.getName(), KeepDirStructure);
}else{
compress(file, zos, file.getName(), KeepDirStructure);
}
}
}
}
}
/**
* @Title: toZip1
* @Description: TODO(压缩成ZIP)
* @param srcDir 压缩文件夹路径
* @param out 压缩文件输出流
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @return void 返回类型
* @throws RuntimeException 压缩失败会抛出运行时异常
*/
public static void toZip1(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException{
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
//设置文件名编码方式(将系统的ZIP编码格式设置为系统文件名编码方式,否则中文名称文件名乱码及解压时报异常.)
zos.setEncoding(System.getProperty("sun.jnu.encoding"));
File sourceFile = new File(srcDir);
compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
long end = System.currentTimeMillis();
System.out.println("压缩完毕,共耗时:"+(end-start)/1000+"秒");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if (null != zos) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @Title: toZip
* @Description: TODO(压缩成ZIP 方法2)
* @param srcFiles 需要压缩的文件列表
* @param out 压缩文件输出流
* @throws RuntimeException 压缩失败会抛出运行时异常
* @return void 返回类型
*/
public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
FileInputStream fis = null;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte buf[] = new byte[BUFFER_SIZE];
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
fis = new FileInputStream(srcFile);
while((len = fis.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
fis.close();
}
long end = System.currentTimeMillis();
System.out.println("解压完毕,共耗时:"+(end-start)/1000+"秒");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e){
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if (null != zos) {
try {
zos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
//测试toZip1压缩
/*String srcPath = "F:\\我的工具\\你好.txt";
try {
FileOutputStream fos1 = new FileOutputStream(new File("f:\\我的工具\\test01.zip"));
toZip1(srcPath, fos1, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
//测试解压
unzip("F:\\我的工具\\test01.zip", "F:\\");
}
}