ZIP/RAR解压缩工具类方法,以此作为自己的第一篇博客,也算作为一个记录吧。
/**
* 上传打包文件(支持rar和zip),解压到服务器/本地
* @param folderPath 路径,以“/”结尾
* @param file 文件
* @param fileType 文件类型(rar/zip)
* @return
* @throws Exception
*/
public static String putPackObject(String folderPath,File file,String fileType) throws Exception{
if(folderPath==null || file==null || fileType==null) return null;
//文件夹名(返回目录参数)
String destDirName = folderPath+file.getName().substring(0,file.getName().lastIndexOf("."))+"/";
try {
//解压文件
if("rar".equals(fileType.toLowerCase())){
Archive a = null;
FileOutputStream fos = null;
try {
a = new Archive(file);
FileHeader fh = a.nextFileHeader();
while (fh != null) {
if (!fh.isDirectory()) {//文件
String fileName = null;
if(fh.isUnicode()){//解決中文乱码
fileName = fh.getFileNameW().trim().replace("\\", "/");
}else{
fileName = fh.getFileNameString().trim().replace("\\", "/");
}
//文件类型验证
if(fileName.toLowerCase().endsWith(".rar") || fileName.toLowerCase().endsWith(".zip")){
throw new Exception("rar file error");
}
//本地文件路径
String destFilePath = destDirName+fileName;
//创建本地文件
File f = new File(destFilePath.replace("/", File.separator));
if(!f.exists() && !f.isDirectory()) {
//判断目标文件所在的目录是否存在
if(!f.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
if(!f.getParentFile().mkdirs()) throw new Exception("create folder error");
}
}else{
fh = a.nextFileHeader();
continue;
}
fos = new FileOutputStream(f);
a.extractFile(fh, fos);
fos.close();
fos = null;
}
fh = a.nextFileHeader();
}
a.close();
a = null;
} catch (Exception e) {
throw e;
} finally {
if (a!=null) {
a.close();
a = null;
}
}
}else if("zip".equals(fileType.toLowerCase())){
ZipFile readfile = null;
try {
//文件
readfile = new ZipFile(file);
Enumeration<ZipEntry> takeentrie = readfile.getEntries();
ZipEntry zipEntry = null;
while (takeentrie.hasMoreElements()){
zipEntry = (ZipEntry) takeentrie.nextElement();
InputStream in = null;
FileOutputStream out = null;
try {
if(!zipEntry.isDirectory()){//文件
String filePath = zipEntry.getName().replace(File.separator, "/");
//文件类型验证
if(filePath.toLowerCase().endsWith(".zip") || filePath.toLowerCase().endsWith(".rar")){
throw new Exception("zip file error");
}
//本地文件路径
String destFilePath = destDirName + filePath;
//创建本地文件
File unpackfile = new File(destFilePath.replace("/", File.separator));
if(!unpackfile.exists() && !unpackfile.isDirectory()) {
//判断目标文件所在的目录是否存在
if(!unpackfile.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
if(!unpackfile.getParentFile().mkdirs()) throw new Exception("create folder error");
}
}else{
continue;
}
in = readfile.getInputStream(zipEntry);
out = new FileOutputStream(unpackfile);
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
}
} catch (IOException ex) {
throw new IOException("extract file error:" + ex.toString());
} finally {
if (in!=null) {
in.close();
in=null;
}
if (out!=null) {
out.close();
out=null;
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
throw new IOException("extract file error:" + ex.toString());
} finally {
if (readfile != null) {
readfile.close();
}
}
}else{
throw new Exception("file type error");
}
}catch (Exception e) {
throw new Exception(e.getMessage());
}
return destDirName;
}
注:该方法只是简单的解压缩文件,权当做参考,具体功能代码还需根据业务需求来写。
相关JAR包
1、ant-1.9.7.jar(解压zip)
2、junrar-0.7.jar(解压rar)