首页 > 代码库 > Java代码中解压RAR文件
Java代码中解压RAR文件
- [java] view plaincopy
- import java.io.File;
- import java.io.FileOutputStream;
- import de.innosystec.unrar.Archive;
- import de.innosystec.unrar.rarfile.FileHeader;
- public class UnRARTools {
- public void unrar(File sourceRar, File destDir) throws Exception {
- Archive archive = null;
- FileOutputStream fos = null;
- System.out.println("Starting...");
- try {
- archive = new Archive(sourceRar);
- FileHeader fh = archive.nextFileHeader();
- int count = 0;
- File destFileName = null;
- while (fh != null) {
- System.out.println((++count) + ") " + fh.getFileNameString());
- String compressFileName = fh.getFileNameString().trim();
- destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName);
- if (fh.isDirectory()) {
- if (!destFileName.exists()) {
- destFileName.mkdirs();
- }
- fh = archive.nextFileHeader();
- continue;
- }
- if (!destFileName.getParentFile().exists()) {
- destFileName.getParentFile().mkdirs();
- }
- fos = new FileOutputStream(destFileName);
- archive.extractFile(fh, fos);
- fos.close();
- fos = null;
- fh = archive.nextFileHeader();
- }
- archive.close();
- archive = null;
- System.out.println("Finished !");
- } catch (Exception e) {
- throw e;
- } finally {
- if (fos != null) {
- try {
- fos.close();
- fos = null;
- } catch (Exception e) {
- //ignore
- }
- }
- if (archive != null) {
- try {
- archive.close();
- archive = null;
- } catch (Exception e) {
- //ignore
- }
- }
- }
- }
- }
需要引用到以下两个lib.
java-unrar-0.5.jar
http://www.java2s.com/Code/JarDownload/java/java-unrar-0.5.jar.zip
apache-commons-logging.jar
http://www.java2s.com/Code/JarDownload/apache-commons/apache-commons-logging.jar.zip
Java代码中解压RAR文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。