package com.yunmall.framework.core.util;
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.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.UUID;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import com.yunmall.framework.beans.ZipResultBean;
import com.yunmall.framework.core.enums.Symbol;
import com.yunmall.framework.core.log.LogTrace;
/**
* 压缩、解压文件工具类
*
* @version 1.0
* @createDate 2012-8-6
* @modifyDate 2012-8-14
*/
public class ZipUtil {
private static final LogTrace log = new LogTrace("VelocityUtil");
private static Properties coreProperties = null;
static {
if (coreProperties == null) {
try {
coreProperties = PropertiesUtil.getProperties("core.properties");
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
/**
* 压缩文件
*
* @param src
* 要压缩的文件
* @param dest
* 压缩后的路径
* @return 压缩文件信息
*/
public static ZipResultBean zip(String src, String dest) {
File srcpath = new File(src);
ZipOutputStream out = null;
Date nowDateTime = DateTimeUtil.getCurrentDate();
String zipPath = DateTimeUtil.format(nowDateTime, "yyyyMMdd");
String zipName = UUID.randomUUID().toString() + ".zip";
ZipResultBean result = new ZipResultBean(zipPath, zipName);
dest = dest + File.separator + zipPath;
File path = new File(dest);
if (!path.exists()) {
path.mkdir();
}
dest = dest + File.separator + zipName;
try {
out = new ZipOutputStream(new FileOutputStream(new File(dest))); // 创建压缩输出流
out.setEncoding(coreProperties.get("zip.encoding").toString().trim());
compress(srcpath, out, ""); // 开始压缩
} catch (FileNotFoundException ex) {
} catch (IOException ex1) {
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ex2) {
}
}
}
return result;
}
/**
* 递归压缩文件
*
* @param src
* @param out
* @param base
* @throws IOException
*/
private static void compress(File src, ZipOutputStream out, String base) throws IOException {
if (src.isDirectory()) { // 如果是目录
File[] files = src.listFiles(); // 列举出目录中所有的内容
out.putNextEntry(new ZipEntry(base + Symbol.Backslash.toString())); // 放入一级目录
base = base.length() == 0 ? "" : base + Symbol.Backslash.toString();
for (int i = 0; i < files.length; i++) {
compress(files[i], out, base + files[i].getName());
}
} else { // 如果是文件
if ("".equals(base)) {
out.putNextEntry(new ZipEntry(base + File.separator));
base = src.getName();
}
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(src);
byte[] data = new byte[4096];
int b;
while ((b = in.read(data)) != -1) {
out.write(data, 0, b);
}
in.close();
}
}
/**
* 压缩文件到默认的路径下
*
* @param source
* 源路径,文件或者目录
* @return ZipBean
* @throws IOException
* IO异常
*/
public static ZipResultBean zip(String source) throws IOException {
if (coreProperties == null) {
coreProperties = PropertiesUtil.getProperties("core.properties");
}
return zip(source, coreProperties.get("zip.targetpath").toString().trim());
}
/**
* 解压ZIP文件
*
* @param source
* 源路径
* @param dest
* 目标路径
* @throws IOException
* IO异常
*/
@SuppressWarnings("rawtypes")
public static void unZip(String source, String dest) throws IOException {
if (StringUtil.isEmpty(source) || StringUtil.isEmpty(dest)) {
return;
}
ZipFile zip = new ZipFile(source, coreProperties.get("zip.encoding").toString().trim());
Enumeration en = zip.getEntries();
ZipEntry entry = null;
byte[] buffer = new byte[8192];
int length = -1;
InputStream input = null;
BufferedOutputStream bos = null;
File file = null;
while (en.hasMoreElements()) {
entry = (ZipEntry) en.nextElement();
if (entry.isDirectory()) {
File emptyDir = new File(dest + "/" + entry.getName());
if (!emptyDir.exists()) {
emptyDir.mkdirs();
}
continue;
}
input = zip.getInputStream(entry);
file = new File(dest, entry.getName());
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
while (true) {
length = input.read(buffer);
if (length == -1) {
break;
}
bos.write(buffer, 0, length);
}
bos.close();
input.close();
}
zip.close();
}
/**
* 解压指定zip文件到默认解压目录
*
* @param source
* 要解压的zip文件
* @return 解压目录
* @throws IOException
* IO异常
*/
public static String unZip(String source) throws IOException {
if (StringUtil.isEmpty(source)) {
return null;
}
File unzipDefaulePath = new File(coreProperties.get("unzip.sources").toString(),
coreProperties.get("zip.encoding").toString().trim());
if (!unzipDefaulePath.exists()) {
unzipDefaulePath.mkdir();
}
String unzipPath = "";
if (source.lastIndexOf(Symbol.Backslash.toString()) != -1) {
unzipPath = source.substring(source.lastIndexOf(Symbol.Backslash.toString()) + 1,
source.length()).replace(".zip", "");
}
unzipPath = unzipDefaulePath + File.separator + unzipPath;
unZip(source, unzipPath);
return unzipPath;
}
/**
* 将默认压缩文件下的指定文件解压缩文件到默认文件夹
*
* @param filenName
* 文件名,如20120820/test.zip或者test.zip
* @return 解压目录
* @throws IOException
* IO异常
*/
public static String unZipDefault(String filenName) throws IOException {
if (StringUtil.isEmpty(filenName)) {
return null;
}
if (coreProperties == null) {
coreProperties = PropertiesUtil.getProperties("core.properties");
}
File unzipDefaulePath = new File(coreProperties.get("unzip.sources").toString());
if (!unzipDefaulePath.exists()) {
unzipDefaulePath.mkdir();
}
String unzipPath = "";
if (filenName.lastIndexOf(Symbol.Backslash.toString()) != -1) {
unzipPath = filenName.substring(filenName.lastIndexOf(Symbol.Backslash.toString()) + 1,
filenName.length()).replace(".zip", "");
} else {
unzipPath = filenName.replace(".zip", "");
}
unzipPath = unzipDefaulePath + File.separator + unzipPath;
String filePath = coreProperties.get("zip.targetpath").toString() + File.separator
+ filenName;
unZip(filePath, unzipPath);
return unzipPath;
}
}
压缩、解压文件工具类
最新推荐文章于 2023-09-07 11:49:22 发布