解压zip类型压缩包
/**
* 解压zip到指定目录
* @author hsj
* @param zipFile
* @param targetFilePath
* @throws IOException
*/
@SuppressWarnings({ "rawtypes" })
public static void unZipFiles(File zipFile, String targetFilePath) {
File pathFile = new File(targetFilePath);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
ZipFile zip=null;
try {
//中文目录处理
zip = new ZipFile(zipFile,Charset.forName("GBK"));
} catch (ZipException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
for(Enumeration entries = zip.entries();entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
String filePath=targetFilePath+File.separator+zipEntryName;
File file = new File(filePath);
//是文件夹则创建文件夹
if(entry.isDirectory()){
if (!file.exists()) {
file.mkdirs();
}
continue;
}else{
InputStream in=null;
try {
in = zip.getInputStream(entry);
//写入本地
writeFile(filePath, in);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
解压rar类型压缩包(rar分两种情况,rar5版本的解压未找到可使用类库,所以使用命令)
rar5以下用代码的话需要引入依赖
<!-- 解压rar start-->
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>3.0.0</version>
</dependency>
<!-- 解压rar end -->
/**
* 解压rar到指定目录
* @author hsj
* @param rarFile
* @param targetFilePath
* @throws Exception
*/
public static void unRarFiles(File rarFile, String targetFilePath){
File pathFile = new File(targetFilePath);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
Archive archive=null;
try {
archive = new Archive(new FileInputStream(rarFile));
FileHeader fileHeader = archive.nextFileHeader();
if(fileHeader!=null){
while (fileHeader != null) {
if (fileHeader.isDirectory()) {
fileHeader = archive.nextFileHeader();
continue;
}
String tempFilePath=fileHeader.getFileNameW();
if(ToolUtil.isEmpty(tempFilePath)){
tempFilePath=fileHeader.getFileNameString();
}
File out = new File(targetFilePath+File.separator+tempFilePath);
if (!out.exists()) {
if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
archive.extractFile(fileHeader, os);
os.close();
fileHeader = archive.nextFileHeader();
}
}else{
boolean result=unRarByCommand(rarFile, targetFilePath);
log.error("解压rar文件失败,尝试命令形式解压"+result);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(archive!=null){
try {
archive.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String RAR_PATH_WIN="\"C:\\Program Files\\WinRAR\\UnRAR.exe\"";
private static String RAR_PATH_LINUX="rar ";
/**
* 解压rar到指定目录 使用命令
* @author hsj
* @param rarFile
* @param targetFilePath
* @throws Exception
*/
public static boolean unRarByCommand(File rarFile, String target) {
boolean bool = false;
if(!rarFile.exists()){
return false;
}
String rootPath="";
if(OsUtil.IS_WINDOWS){
rootPath=RAR_PATH_WIN;
}else if(OsUtil.IS_LINUX){
rootPath=RAR_PATH_LINUX;
}
String cmd = rootPath + " X " + rarFile.getAbsolutePath() + " "+target;
try {
System.out.println(cmd);
Process proc = Runtime.getRuntime().exec(cmd);
if (proc.waitFor() != 0) {
if (proc.exitValue() == 0) {
bool = false;
}
} else {
bool = true;
}
proc.destroy();
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}