才发现java库没有对rar5压缩算法实现,网上找了一大堆资料,基本都是调用命令的方式,要集成到项目里是很麻烦的,所以一直奔着java解压rar5的目标去找,终于在github上找到了已经实现好的库,希望能帮到大家。
这个库实际是github官方自己实现的,估计是内部项目需要。
按照我下面的步骤就轻松可以完成:
1.pom中添加以下依赖:
<!-- https://mvnrepository.com/artifact/com.github.axet/java-unrar -->
<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>
2.java中解压rar5文件的例子:
package rar5;
import java.io.IOException;
import java.io.RandomAccessFile;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
public class RAR5Test {
public static void main(String[] args) throws IOException {
String rarDir = "D:\\keys\\rar5.rar";
String outDir = "D:\\keys\\rar5";
RandomAccessFile randomAccessFile = null;
IInArchive inArchive = null;
// 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile
randomAccessFile = new RandomAccessFile(rarDir, "r");
inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
int[] in = new int[inArchive.getNumberOfItems()];
for (int i = 0; i < in.length; i++) {
in[i] = i;
}
inArchive.extract(in, false, new ExtractCallback(inArchive, "366", outDir));
}
}
3.自己编写回调ExtractCallback
package rar5;
import net.sf.sevenzipjbinding.*;
import java.io.*;
public class ExtractCallback implements IArchiveExtractCallback {
private int index;
private String packageName;
private IInArchive inArchive;
private String ourDir;
public ExtractCallback(IInArchive inArchive, String packageName, String ourDir) {
this.inArchive = inArchive;
this.packageName = packageName;
this.ourDir = ourDir;
}
@Override
public void setCompleted(long arg0) throws SevenZipException {
}
@Override
public void setTotal(long arg0) throws SevenZipException {
}
@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);
return new ISequentialOutStream() {
public int write(byte[] data) throws SevenZipException {
try {
if (!isFolder) {
System.out.println(path);
File file = new File(ourDir + path);
save2File(file, data);
}
} catch (Exception e) {
e.printStackTrace();
}
return data.length;
}
};
}
@Override
public void prepareOperation(ExtractAskMode arg0) throws SevenZipException {
}
@Override
public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
String path = (String) inArchive.getProperty(index, PropID.PATH);
boolean isFolder = (boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
// if(ZipUtils.checkOnlyGetDir(path) && !isFolder){
// if (extractOperationResult != ExtractOperationResult.OK) {
// StringBuilder sb = new StringBuilder();
// sb.append("解压").append(packageName).append("包的").append(path).append("文件");
// sb.append("失败!");
// log.error(sb.toString());
// }
// }
}
public static boolean save2File(File file, byte[] msg) {
OutputStream fos = null;
try {
File parent = file.getParentFile();
boolean bool;
if ((!parent.exists()) && (!parent.mkdirs())) {
return false;
}
fos = new FileOutputStream(file);
fos.write(msg);
fos.flush();
return true;
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
File parent;
return false;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
}
}
好了,就这么简单。