用于解压,压缩Windows上的压缩文件
解压缩文件方法:FileUtil.java
package com.feike;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.log4j.Logger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
/**
* 解压缩文件Util
* @author: Feike
* @date: 2017年8月6日下午12:56:47
*/
public class FileUtil {
private static Logger logger = Logger.getLogger(FileUtil.class);
/**
* 压缩文件
* @author Feike
* @date 2017年8月6日下午12:57:25
* @param zipFilePath 压缩以后的路径+文件名
* @param srcPathName 要压缩的文件夹或文件路径
*/
public static void zipFiles(String zipFilePath, String srcPathName) {
File zipFile = new File(zipFilePath);
File srcdir = new File(srcPathName);
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "路径不存在");
}
Project prj = new Project();
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
if(srcdir.isDirectory()) {
fileSet.setDir(srcdir);
} else {
fileSet.setFile(srcdir);
}
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
zip.setEncoding("gbk");
zip.addFileset(fileSet);
zip.execute();
logger.debug("---compress files success---");
}
/**
* 解压文件
* @author Feike
* @date 2017年8月6日下午1:01:08
* @param zipFilePath 要解压的文件路径+文件名
* @param fileSavePath 解压后的路径
* @param isDelete 是否删除源解压文件
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static void unZipFiles(String zipFilePath, String fileSavePath, boolean isDelete) throws Exception{
try {
File f = new File(zipFilePath);
if ((!f.exists()) && (f.length() <= 0)) {
throw new RuntimeException("要解压的文件不存在!");
}
ZipFile zipFile = new ZipFile(f, "gbk");
String strPath, gbkPath, strtemp;
strPath = fileSavePath;
Enumeration<ZipEntry> e = zipFile.getEntries();
while (e.hasMoreElements()) {
org.apache.tools.zip.ZipEntry zipEnt = e.nextElement();
gbkPath = zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;
if (zipEnt.isDirectory()) {
File dir = new File(strtemp);
if(!dir.exists()){
dir.mkdirs();
}
continue;
} else {
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
String strsubdir = gbkPath;
for (int i = 0; i < strsubdir.length(); i++) {
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator
+ strsubdir.substring(0, i);
File subdir = new File(temp);
if (!subdir.exists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
byte[] buff = new byte[1024];
while ((len = bis.read(buff)) != -1) {
bos.write(buff, 0, len);
}
bos.close();
fos.close();
}
}
zipFile.close();
} catch (Exception e) {
logger.error("解压失败", e);
throw e;
}
/**
* 源文件有可能被别的进程占用,导致无法删除。
*/
if(isDelete){
boolean flag = new File(zipFilePath).delete();
logger.debug("是否成功删除源文件: " + flag);
}
logger.debug("compress files success");
}
public static void main(String[] args) throws Exception {
zipFiles("D:\\test.zip", "D:\\test");
}
}
log日志:log4j.properties
log4j.rootLogger=WARN,stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
#日志输出到指定文件
log4j.appender.R.File=D\:\\mylog.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# com.feike 包线的log日志级别是 DEBUG
log4j.logger.com.feike=DEBUG
jar 包