直接上代码:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/*
* 对特定文件进行解压缩
*/
public class ZipAndUnZipUtil {
private ZipAndUnZipUtil() {
}
private static ZipAndUnZipUtil zipSingleInstance;
/*
* 生成实例
*/
public static ZipAndUnZipUtil getInstance() {
if (null == zipSingleInstance) {
zipSingleInstance= new ZipAndUnZipUtil();
}
return zipSingleInstance;
}
/*
* 解压文件
* @param srcPath 源压缩包路径
* @param despath 解压后存储的目录
* @return 解压后存储的路径目录
*/
@SuppressWarnings("unchecked")
public String unZip(String srcPath, String desPath) {
if ("".equals(srcPath) || null == srcPath) {
System.out.println("解压的文件的路径为空");
return null;
}
if ("".equals(srcPath) || null == desPath) {
System.out.println("文件解压后存储路径为");
return null;
}
File srcFile = new File(srcPath);
File desFile = new File(desPath);
//创建指定目录,如果其不存在的情况下
if (!desFile.exists()) {
if (!desFile.mkdirs()) {
System.out.println("指定目录创建失败");
return null;
}
}
try {
//读取zip文件
ZipFile zipTest = new ZipFile(srcFile);
Enumeration entries = (Enumeration) zipTest
.entries();
ZipEntry entry = null;
while (entries.hasMoreElements()) {
entry = entries.nextElement();
if (entry.isDirectory()) {
//创建目录
File dirFile=new File(desPath+File.separator+entry.getName());
dirFile.mkdirs();
} else {
//解压文件
BufferedInputStream inputStream = new BufferedInputStream(zipTest.getInputStream(entry));
BufferedOutputStream out =new BufferedOutputStream( new FileOutputStream(desPath+File.separator+entry.getName()));
byte[] bytes=new byte[2*1024];
int count;
while((count=inputStream.read(bytes))!=-1){
out.write(bytes, 0, count);
}
inputStream.close();
out.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return desPath;
}
/*
* 压缩文件
*
* @param srcPath 需要压缩的文件路径
* @param despath 压缩文件的存储目录
* @param name 文件名字
* @return desPath 压缩文件结果的存储路径,如果输入路径为空,或者存储路径已经存在,则返回null,
*/
public String zipDocuments(String srcPath, String desPath,String name) {
if ("".equals(srcPath) || null == srcPath) {
System.out.println("压缩的文件的路径为空");
return null;
}
if ("".equals(srcPath) || null == desPath) {
System.out.println("压缩文件的存储路径为");
return null;
}
File srcFile = new File(srcPath);
File desFile = new File(desPath);
// 判断目标目录是否存在,如果不存在就创建
if (!desFile.exists()) {
if (!desFile.mkdirs()) {
System.out.println("存储目录创建失败");
return null;
}
}
try {
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(
desPath + File.separator + name));
int start = srcPath.lastIndexOf(srcFile.getName());
// 压缩文件
zipDirAndFile(srcFile, outZip, start);
outZip.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return desPath + File.separator + name;
}
/*
* 压缩文件及目录
*/
private void zipDirAndFile(File file, ZipOutputStream out, int start) {
if (null == file | null == out) {
throw new NullPointerException("文件或ZipOutputStream引用为空");
}
File[] files;
// 如果file对象是目录
if (file.isDirectory()) {
files = file.listFiles();
try {
// 生成压缩目录文件
String dirPath = file.getPath().substring(start);
out.putNextEntry(new ZipEntry(absToreDirPath(dirPath)));
out.closeEntry();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (File f : files) {
zipDirAndFile(f, out, start);
}
} else {
// 如果file对象时文件
BufferedInputStream inZip;
byte[] buff;
try {
inZip = new BufferedInputStream(new FileInputStream(file));
buff = new byte[5 * 1024];
ZipEntry zipEntry = new ZipEntry(new String(file.getPath()
.substring(start)));
out.putNextEntry(zipEntry);
int count;
while ((count = inZip.read(buff, 0, buff.length)) != -1) {
out.write(buff, 0, count);
}
inZip.close();
out.closeEntry();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 路径转换成相对路径 start:相对路径起始的位置
*/
private String absToreDirPath(String absPath) {
char[] pathChars = absPath.toCharArray();
for (int i = 0; i < pathChars.length; i++) {
if (File.separatorChar == pathChars[i]) {
pathChars[i] = '/';
}
}
return new String(pathChars) + "/";
}
}