Java代码中解压RAR文件

本文提供了一个使用Java实现的解压RAR文件的工具类,包括如何导入必要的库,使用Archive类来读取RAR文件,并将文件解压到指定目录。详细介绍了文件头的解析、判断是否为目录、创建目标目录及文件输出流等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. [java]  view plain copy
     
    1. import java.io.File;  
    2. import java.io.FileOutputStream;  
    3.   
    4. import de.innosystec.unrar.Archive;  
    5. import de.innosystec.unrar.rarfile.FileHeader;  
    6.   
    7. public class UnRARTools {  
    8.   
    9.     public void unrar(File sourceRar, File destDir) throws Exception {  
    10.         Archive archive = null;  
    11.         FileOutputStream fos = null;  
    12.         System.out.println("Starting...");  
    13.         try {  
    14.             archive = new Archive(sourceRar);  
    15.             FileHeader fh = archive.nextFileHeader();  
    16.             int count = 0;  
    17.             File destFileName = null;  
    18.             while (fh != null) {  
    19.                 System.out.println((++count) + ") " + fh.getFileNameString());  
    20.                 String compressFileName = fh.getFileNameString().trim();  
    21.                 destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName);  
    22.                 if (fh.isDirectory()) {  
    23.                     if (!destFileName.exists()) {  
    24.                         destFileName.mkdirs();  
    25.                     }  
    26.                     fh = archive.nextFileHeader();  
    27.                     continue;  
    28.                 }   
    29.                 if (!destFileName.getParentFile().exists()) {  
    30.                     destFileName.getParentFile().mkdirs();  
    31.                 }  
    32.                 fos = new FileOutputStream(destFileName);  
    33.                 archive.extractFile(fh, fos);  
    34.                 fos.close();  
    35.                 fos = null;  
    36.                 fh = archive.nextFileHeader();  
    37.             }  
    38.   
    39.             archive.close();  
    40.             archive = null;  
    41.             System.out.println("Finished !");  
    42.         } catch (Exception e) {  
    43.             throw e;  
    44.         } finally {  
    45.             if (fos != null) {  
    46.                 try {  
    47.                     fos.close();  
    48.                     fos = null;  
    49.                 } catch (Exception e) {  
    50.                     //ignore  
    51.                 }  
    52.             }  
    53.             if (archive != null) {  
    54.                 try {  
    55.                     archive.close();  
    56.                     archive = null;  
    57.                 } catch (Exception e) {  
    58.                     //ignore  
    59.                 }  
    60.             }  
    61.         }  
    62.     }  
    63.   
    64. }  

     

     

    需要引用到以下两个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

转载于:https://www.cnblogs.com/yuanjun1/p/3929971.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值