依赖的jar包 java-unrar-0.3.jar
在maven仓库中,http://mvnrepository.com/artifact/com.github.junrar/junrar/0.7
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.exception.RarException;
import de.innosystec.unrar.rarfile.FileHeader;
public class UnRAR {
/**
* args
* IOException
* RarException
*/
public static void main(String[] args) throws RarException, IOException {
//压缩文件
String rarPath = "E:\\zip.rar";
//解压到这个目录
String dstDirectoryPath = "E:\\11";
File dstDiretory = new File(dstDirectoryPath);
if (!dstDiretory.exists()) {
dstDiretory.mkdirs();
}
Archive a = new Archive(new File(rarPath));
if (a != null) {
a.getMainHeader().print(); //打印文件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
//文件
File out = new File(dstDirectoryPath + File.separator + fh.getFileNameString().trim());
System.out.println(out.getAbsolutePath());
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();
fh = a.nextFileHeader();
}
}
a.close();
}
}