rar zip解压

wylc123 1年前 ⋅ 582 阅读

rar zip解压

JAVA使用 java-unrar-0.3.jar 解压rar,并且解决中文乱码

综合代码

package cnki.bdmsda.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;

import de.innosystec.unrar.Archive;
import de.innosystec.unrar.exception.RarException;
import de.innosystec.unrar.rarfile.FileHeader;    
     
public class DeCompressUtil {    
   /**  
    * 解压zip格式压缩包  
    * 对应的是ant.jar  
    */   
   private static 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));    
           /*  
           ant下的zip工具默认压缩编码为UTF-8编码,  
           而winRAR软件压缩是用的windows默认的GBK或者GB2312编码  
           所以解压缩时要制定编码格式  
           */   
           e.setEncoding("gbk");    
           e.execute();    
       }catch(Exception e){    
           throw e;    
       }    
   }    
   /**  
    * 解压rar格式压缩包。  
    * 对应的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar  
    */   
   public static void unrar(String srcPath,String unrarPath) throws RarException, IOException, Exception{
		File srcFile = new File(srcPath);
		if(null == unrarPath || "".equals(unrarPath)){
			unrarPath = srcFile.getParentFile().getPath();
		}
		// 保证文件夹路径最后是"/"或者"\"
		char lastChar = unrarPath.charAt(unrarPath.length() - 1);
		if (lastChar != '/' && lastChar != '\\') {
			unrarPath += File.separator;
		}
		unrar(srcFile, unrarPath);
	}

	private static void unrar(File srcFile,String unrarPath) throws RarException, IOException, Exception{
       FileOutputStream fileOut = null;
       Archive rarfile = null;

       try{
	        rarfile = new Archive(srcFile);
	        FileHeader fh = rarfile.nextFileHeader();
	        while(fh!=null){

	        	String entrypath = "";
	        	if(fh.isUnicode()){//解決中文乱码
	        		entrypath = fh.getFileNameW().trim();
	        	}else{
	        		entrypath = fh.getFileNameString().trim();
	        	}
	        	entrypath = entrypath.replaceAll("\\\\", "/");

	        	File file = new File(unrarPath + entrypath);
	        	if(fh.isDirectory()){
	        		file.mkdirs();
	        	}else{
	            	File parent = file.getParentFile();
	                if(parent!=null && !parent.exists()){
	                    parent.mkdirs();
	                }
	                fileOut = new FileOutputStream(file);
	                rarfile.extractFile(fh, fileOut);
	                fileOut.close();
	        	}
	        	fh = rarfile.nextFileHeader();
	        }
	        rarfile.close();

       } catch (Exception e) {
			throw e;
		} finally {
			if (fileOut != null) {
				try {
					fileOut.close();
					fileOut = null;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (rarfile != null) {
				try {
					rarfile.close();
					rarfile = null;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

   /**  
    * 解压缩  
    */   
   public static 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")){    
           DeCompressUtil.unzip(sourceFile, destDir);    
        }else if(type.equals("rar")){    
            DeCompressUtil.unrar(sourceFile, destDir);    
        }else{    
            throw new Exception("只支持zip和rar格式的压缩包!");    
        }    
    }    
   
   	public static void main(String[] args) {
   		try {
			deCompress("D:/fileupload/train_corpus.rar","D:/fileupload/");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}  
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: