群里一位朋友的解压RAR的代码,留着以备参考

来源:互联网 发布:linux sh 定义变量 编辑:程序博客网 时间:2024/05/21 18:38

  慕容火月提供的部分代码

  Archive rar = new Archive(new File("d://2345.rar"));
  FileHeader fh = rar.nextFileHeader();
  while(fh != null){ 
   System.out.println("fh.name="+fh.getFileNameString());
            File out = new File("d://",fh.getFileNameString());
            FileOutputStream os = new FileOutputStream(out);
            rar.extractFile(fh, os);
            os.close();
            fh=rar.nextFileHeader();
  }

 

 

以下是j2ee北京提供的方法,需要用到java-urar-0.3.jar这个jar包

 

源码如下:

      

 

      /**
     * 解压rar格式的压缩文件到指定目录下
     * @param rarFileName 压缩文件
     * @param extPlace 解压目录
     * @param repeatFile 重复路径
     * @throws Exception
     * @author Every
     */
     public synchronized void unrar(String rarFileName, String extPlace,List repeatFile) throws Exception{
              File f = new File(rarFileName);
              FileOutputStream os=null;
              Archive a=null;
              try {
                  a = new Archive(f);
              } catch (RarException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
              } catch (IOException e) {
                 // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              extPlace=(extPlace==null||extPlace.trim().equals(""))?f.getParent()+File.separator+f.getName().replace(".", "#").split("#")[0]:extPlace;
//  System.out.println(extPlace);
  File ext=new File(extPlace);
  if(!ext.exists()){
   ext.mkdirs();
  }
  if(a!=null){
   a.getMainHeader().print();
   FileHeader fh = a.nextFileHeader();
   while(fh!=null){
    try {
     File out = new File(extPlace+File.separator+fh.getFileNameString().trim());
     if (out.exists()&&repeatFile!=null) {
      File rFile=new File(extPlace+File.separator+"tempFile");
      if(rFile.exists())rFile.mkdirs();
      repeatFile.add(fh.getFileNameString().trim());
      out=new File(extPlace+File.separator+"tempFile"+File.separator+fh.getFileNameString().trim());
      os = new FileOutputStream(out);
     }else{
      os = new FileOutputStream(out);
     }
     a.extractFile(fh, os);
     os.close();
    } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (RarException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    fh=a.nextFileHeader();
   }
  }
 }
 /**
     * 解压rar格式的压缩文件到当前文件所在目录下
     * @param rarFileName 压缩文件
     * @param repeatFile 重复路径
     * @throws Exception
     * @author Every
     */
 public synchronized void unrar(String rarFileName,List repeatFile) throws Exception{
  unrar(rarFileName,null,repeatFile);
 }
 
 /**
     * 解压rar格式的压缩文件到当前文件所在目录下
     * @param rarFileName 压缩文件
     * @param extPlace 解压目录
     * @throws Exception
     * @author Every
     */
 public synchronized void unrar(String rarFileName,String extPlace) throws Exception{
  unrar(rarFileName,extPlace,null);
 }
 
 /**
     * 解压rar格式的压缩文件到当前文件所在目录下
     * @param rarFileName 压缩文件
     * @param delete 是否删除源文件
     * @throws Exception
     * @author Every
     */
 public synchronized void unrar(String rarFileName) throws Exception{
  unrar(rarFileName,null,null);
 }
 
 
 
 /**
  * 执行实例
  * @param args
  */
 public static void main(String[] args) throws Exception {
  Decompression decompression=new Decompression();
//  decompression.unzipFile("c:/test.zip");
//  decompression.unzip("c:/test.zip","c:/test22");
//  decompression.zip("c:/test22", "c:/test222.zip");
  decompression.unrar("c:/tests.rar",null,new ArrayList());
 }

 

 

懒的排格式了= =|| 用到的时候再说