有个项目需要解压rar,上网搜索一下大家都在用? java-unrar-0.3.jar ,于是写了一个util方法解压rar
?
class="java" name="code"> public static void unrar(String srcPath,String unrarPath,boolean incRarDir) throws RarException, IOException{ unrar(new File(srcPath), unrarPath, incRarDir); } public static void unrar(File srcFile,String unrarPath,boolean incRarDir) throws RarException, IOException{ if(StringUtil.isEmptyString(unrarPath)){ unrarPath = srcFile.getParentFile().getPath(); } if(incRarDir){ File dir = new File(unrarPath+"/"+FileUtil.getFileNameWithoutType(srcFile.getName())); dir.mkdir(); unrarPath = dir.getPath(); } System.out.println("unrar file to :"+unrarPath); FileOutputStream fileOut; File file; Archive rarfile = new Archive(srcFile); FileHeader entry = rarfile.nextFileHeader(); while(entry!=null){ String entrypath = entry.getFileNameString().trim(); entrypath=entrypath.replaceAll("\\\\", "/"); file = new File(unrarPath+"/"+entrypath); System.out.println("unrar entry file :"+file.getPath()); if(entry.isDirectory()){ file.mkdirs(); }else{ File parent = file.getParentFile(); if(parent!=null && !parent.exists()){ parent.mkdirs(); } fileOut = new FileOutputStream(file); rarfile.extractFile(entry, fileOut); fileOut.close(); } entry = rarfile.nextFileHeader(); } rarfile.close(); }
?
?
经过验证,解压包中如果没有中文名是正常的,但是一旦带有中文名就出现乱码了,看了一下FileHeader类的方法提示,发现有个isUnicode的方法,估计是用来判断文件名是不是Unicode字符的,而且还有一个方法获得压缩包里的文件名的 getFileNameW,经过反复试验,应该这样写才能解决乱码问题
String entrypath = ""; if(entry.isUnicode()){//Unicode文件名使用getFileNameW entrypath = entry.getFileNameW().trim(); }else{ entrypath = entry.getFileNameString().trim(); }
?乱码问题解决
?最后附上正确的使用源码和 java-unrar-0.3.jar
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import de.innosystec.unrar.Archive; import de.innosystec.unrar.exception.RarException; import de.innosystec.unrar.rarfile.FileHeader; public class RARUtil { public static void unrar(String srcPath,String unrarPath,boolean incRarDir) throws RarException, IOException{ unrar(new File(srcPath), unrarPath, incRarDir); } public static void unrar(File srcFile,String unrarPath,boolean incRarDir) throws RarException, IOException{ if(StringUtil.isEmptyString(unrarPath)){ unrarPath = srcFile.getParentFile().getPath(); } if(incRarDir){ File dir = new File(unrarPath+"/"+FileUtil.getFileNameWithoutType(srcFile.getName())); dir.mkdir(); unrarPath = dir.getPath(); } System.out.println("unrar file to :"+unrarPath); FileOutputStream fileOut; File file; Archive rarfile = new Archive(srcFile); FileHeader entry = rarfile.nextFileHeader(); while(entry!=null){ String entrypath = ""; if(entry.isUnicode()){ entrypath = entry.getFileNameW().trim(); }else{ entrypath = entry.getFileNameString().trim(); } entrypath=entrypath.replaceAll("\\\\", "/"); file = new File(unrarPath+"/"+entrypath); System.out.println("unrar entry file :"+file.getPath()); if(entry.isDirectory()){ file.mkdirs(); }else{ File parent = file.getParentFile(); if(parent!=null && !parent.exists()){ parent.mkdirs(); } fileOut = new FileOutputStream(file); rarfile.extractFile(entry, fileOut); fileOut.close(); } entry = rarfile.nextFileHeader(); } rarfile.close(); } }
?