zoukankan      html  css  js  c++  java
  • java进阶篇解压缩zip rar文件

    package com.cn.teachmobile.utils;
    
    import java.io.File;
    import java.io.FileOutputStream;
    
    import org.apache.tools.ant.Project;
    import org.apache.tools.ant.taskdefs.Expand;
    
    import de.innosystec.unrar.Archive;
    import de.innosystec.unrar.rarfile.FileHeader;
    
    /**
     * @author gongchaobin
     *
     * 解压缩zip rar文件
     */
    public class ZipHelper {
    
       /**
        * 解压zip文件
        * 
        * @param sourceZip 需要解压的文件路径
        * @param destDir 解压存放的路径
        * @throws Exception
        */
       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文件
        * 
        * @param sourceRar
        * @param destDir
        * @throws Exception
        */
       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 destFileName = "";   
                       String destDirName = "";   
                       compressFileName = new String(compressFileName.getBytes("UTF-8"),"GB2312");
                       //非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();}   
               }   
           }   
       }   
    }

    三个包的下载链接:https://files.cnblogs.com/gongcb/libs.zip

  • 相关阅读:
    CentOS 6.5 编译安装Apache2.4
    Linux 服务器安全优化
    yum安装Apache2.4
    HDU 3308 线段树求区间最长连续上升子序列长度
    HDU 3607 线段树+DP+离散化
    POJ 3667 线段树区间合并裸题
    HDU 5726 线段树+dp
    牛客网多校训练第二场D Kth Minimum Clique
    HDU 4325 离散化+树状数组 或者 不使用树状数组
    HDU 2167 状压dp方格取数
  • 原文地址:https://www.cnblogs.com/gongcb/p/2724035.html
Copyright © 2011-2022 走看看