使用代码解压压缩文件,并指定解压后路径
导入依赖
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>java-unrar</artifactId>
<version>1.7.0-8</version>
</dependency>
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding</artifactId>
<version>16.02-2.01</version>
</dependency>
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding-all-platforms</artifactId>
<version>16.02-2.01</version>
</dependency>
FileUtil 文件工具类
public class FileUtil {
public static List<String> unRar(File rarFile,String dstDirectoryPath){
IInArchive archive;
RandomAccessFile randomAccessFile;
try {
randomAccessFile = new RandomAccessFile(rarFile,"r");
archive = SevenZip.openInArchive(null,new RandomAccessFileInStream(randomAccessFile));
int[] in = new int[archive.getNumberOfItems()];
for (int i = 0; i < in.length; i++) {
in[i] = i;
}
archive.extract(in,false,new ExtractCallback(archive,dstDirectoryPath));
archive.close();
randomAccessFile.close();
System.out.println("解压目标文件夹:"+dstDirectoryPath);
List<String> allFileList = getAllFile(dstDirectoryPath,false);
ArrayList<String> resultFileList = new ArrayList<>();
String startString;
String endString;
String finallyString;
for (String s : allFileList) {
if (s.startsWith("/")||s.startsWith("\\")) {
startString = s.substring(0,s.lastIndexOf("\\"));
endString = s.substring(s.lastIndexOf("\\")+1);
finallyString = startString+"\\"+endString;
}else {
// windows去掉盘符
s.substring(2);
startString = s.substring(0,s.lastIndexOf("\\"));
endString = s.substring(s.lastIndexOf("\\")+1);
finallyString = startString+"/"+endString;
}
System.out.println("rar文件解压前缀为:"+startString+",rar文件解压后缀为:"+endString);
// 解决linux路径出现//导致文件路径错误
finallyString = finallyString.replaceAll("//","/");
resultFileList.add(finallyString);
}
System.out.println("rar文件解压文件路径为:"+resultFileList);
return resultFileList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static List<String> getAllFile(String directoryPath,boolean isAddDirectory){
List<String> list = new ArrayList<>();
File baseFile = new File(directoryPath);
if (baseFile.isFile()||!baseFile.exists()) {
return list;
}
File[] files = baseFile.listFiles();
for (File file : files) {
if (file.isDirectory()) {
if (isAddDirectory) {
list.add(file.getAbsolutePath());
}
list.addAll(getAllFile(file.getAbsolutePath(),isAddDirectory));
}else {
list.add(file.getAbsolutePath());
}
}
return list;
}
}
ExtractCallback回调方法
public class ExtractCallback implements IArchiveExtractCallback {
private int index;
private IInArchive inArchive;
private String ourDir;
public ExtractCallback(IInArchive inArchive, String ourDir) {
this.inArchive = inArchive;
this.ourDir = ourDir;
}
@Override
public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
this.index = index;
final String path = (String) inArchive.getProperty(index, PropID.PATH);
final boolean isFolder = (boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
final String[] oldPath = {""};
return new ISequentialOutStream() {
@Override
public int write(byte[] data) throws SevenZipException {
try {
if (!isFolder) {
File file = new File(ourDir + "\\" + path);
if (path.equals(oldPath[0])) {
save2File(file, data, true);
} else {
save2File(file, data, false);
}
oldPath[0] = path;
}
} catch (Exception e) {
e.printStackTrace();
}
return data.length;
}
};
}
@Override
public void prepareOperation(ExtractAskMode extractAskMode) throws SevenZipException {
}
@Override
public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
}
@Override
public void setTotal(long l) throws SevenZipException {
}
@Override
public void setCompleted(long l) throws SevenZipException {
}
public boolean save2File(File file, byte[] msg, boolean append) {
FileOutputStream fos = null;
try {
File parent = file.getParentFile();
boolean bool;
if ((!parent.exists()) && (!parent.mkdirs())) {
return false;
}
fos = new FileOutputStream(file, append);
fos.write(msg);
fos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
主方法
public class TestMain {
public static void main(String[] args) {
String rarFile = "D:\\JavaCode\\testmaven00\\testCompressedFile\\test.rar";
File file = new File(rarFile);
String name = file.getName().substring( file.getName().lastIndexOf(".")+1);
String unRar = "D:\\JavaCode\\testmaven00\\testCompressedFile\\tmp\\";
FileUtil.unRar(file,unRar);
}
}