解压rar文件的时候如果路径里有中文就解压不了,请高手指点。。。
多谢了。。。
- Java code
1 package com.geap.gpwms.test; 2 3 import java.util.*; 4 import java.io.*; 5 import java.net.URLEncoder; 6 7 import org.apache.tools.zip.*; 8 9 10 public class ZipUtil11 {12 private static String unrarCmd = "d:\\Program Files\\WinRAR\\UnRar x "; 13 14 15 16 public static boolean unfile(String zipFile,String outFilePath,int mode){17 boolean flag=false;18 try{ 19 File file = new File(zipFile); 20 String fileName = file.getName(); 21 if(mode == 1) 22 { 23 outFilePath += File.separator; //文件当前路径下 24 }else{ 25 outFilePath += File.separator+fileName.substring(0,fileName.length()-4)+File.separator; 26 } 27 File tmpFileDir = new File(outFilePath); 28 tmpFileDir.mkdirs(); 29 30 31 32 unrarCmd += zipFile + " " + outFilePath;33 34 try {35 Runtime rt = Runtime.getRuntime();36 Process p = rt.exec(unrarCmd);37 BufferedReader br = new BufferedReader(new InputStreamReader( 38 p.getInputStream())); 39 BufferedReader br1 = new BufferedReader(new InputStreamReader( 40 p.getErrorStream())); 41 while ((br.readLine()) != null || br1.readLine() != null) 42 ; 43 p.waitFor(); 44 flag=true;45 } catch (Exception e) {46 System.out.println(e.getMessage());47 } 48 }49 50 }catch(Exception e){51 e.printStackTrace();52 }53 return flag;54 }55 public static void main(String[] args)56 {57 try58 {59 ZipUtil zip=new ZipUtil();60 61 File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\log.rar");62 String zipFile=file.getAbsolutePath();63 String outFilePath=file.getParentFile().getPath();64 int mode=1;65 //测试解压66 zip.unfile(zipFile, outFilePath, mode);67 68 }69 catch (Exception e)70 {71 e.printStackTrace();72 }73 74 }75 76 }
------解决方案--------------------
转码 首先打印出你的文件名称是否是中文正常显示。 在控制台打印出来。如果乱码请强制转码