java 解压zip rar格式压缩包代码


   //解压zip格式压缩包  
   private void unzip(String sourceZip,String destDir) throws Exception{    
       try{    
           Project p = new Project();    
           Expand e = new Expand();    
           e.setProject(p);    
           e.setSrc(new File(sourceZip));    
           e.setOverwrite(false);    
           e.setDest(new File(destDir));      
           e.setEncoding("gbk");    
           e.execute();    
       }catch(Exception e){    
           throw e;    
       }    
   }    


   //解压rar格式压缩包。   
   private void unrar(String sourceRar,String destDir) throws Exception{    
       Archive a = null;    
       FileOutputStream fos = null;    
       try{    
           a = new Archive(new File(sourceRar));    
           FileHeader fh = a.nextFileHeader();    
           while(fh!=null){    
               if(!fh.isDirectory()){    
                   //1 根据不同的操作系统拿到相应的 destDirName 和 destFileName    
                   String compressFileName = fh.isUnicode()?fh.getFileNameW().trim():fh.getFileNameString().trim();    
                   String destFileName = "";    
                   String destDirName = "";    
                   //非windows系统    
                   if(File.separator.equals("/")){    
                       destFileName = destDir + compressFileName.replaceAll("\\\\", "/");    
                       destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));    
                   //windows系统     
                   }else{    
                       destFileName = destDir + compressFileName.replaceAll("/", "\\\\");    
                       destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));    
                   }    
                   //2创建文件夹    
                   File dir = new File(destDirName);    
                   if(!dir.exists()||!dir.isDirectory()){    
                       dir.mkdirs();    
                   }    
                   //3解压缩文件    
                   fos = new FileOutputStream(new File(destFileName));    
                   a.extractFile(fh, fos);    
                   fos.close();    
                   fos = null;    
               }    
               fh = a.nextFileHeader();    
           }    
           a.close();    
           a = null;    
       }catch(Exception e){    
           throw e;    
       }finally{    
           if(fos!=null){    
               try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();}    
           }    
           if(a!=null){    
               try{a.close();a=null;}catch(Exception e){e.printStackTrace();}    
           }    
       }    
   }


  //解压缩  
   public void deCompress(String sourceFile,String destDir) throws Exception{    
       //保证文件夹路径最后是"/"或者"\"    
       char lastChar = destDir.charAt(destDir.length()-1);    
       if(lastChar!='/'&&lastChar!='\\'){    
           destDir += File.separator;    
       }    
       //根据类型,进行相应的解压缩    
       String type = sourceFile.substring(sourceFile.lastIndexOf(".")+1);    
       if(type.equals("zip")){    
           unzip(sourceFile, destDir);    
        }else if(type.equals("rar")){    
            unrar(sourceFile, destDir);    
        }else{    
            throw new Exception("只支持zip和rar格式的压缩包!");    
        }    
    }

猜你喜欢

转载自blog.csdn.net/yangymy/article/details/80374100
今日推荐