rar解压因为存在中文名字乱码问题。

/**  
        *
        *  解压rar格式压缩包。
        */
           private static 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.getFileNameString().trim();
                           String compressFileName=  fh.getFileNameW().trim();  
                            if(!existZH(compressFileName)){  
                                compressFileName = 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();}    
                   }    
               }    
           }
             //判断rar 是否存在中文
             public static boolean existZH(String str) {  
                 String regEx = "[\\u4e00-\\u9fa5]";  
                 Pattern p = Pattern.compile(regEx);  
                 Matcher m = p.matcher(str);  
                 while (m.find()) {  
                     return true;  
                 }  
                 return false;  
             } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0NE-PIECE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值