国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

詳解java解壓縮zip和rar的工具類使用教程

2019-11-14 14:49:40
字體:
來源:轉載
供稿:網友

詳解java解壓縮zip和rar的工具類使用教程直接上代碼

package decomPRess;         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;         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       */       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 = "";                        //非windows系統                        if(File.separator.equals("/")){                            destFileName = destDir + compressFileName.replaceAll("http:////", "/");                            destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));                        //windows系統                         }else{                            destFileName = destDir + compressFileName.replaceAll("/", "http:////");                            destDirName = destFileName.substring(0, destFileName.lastIndexOf("http://"));                        }                        //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();}                }            }        }        /**       * 解壓縮       */       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格式的壓縮包!");             }         }     }

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邢台市| 禹城市| 永城市| 沙湾县| 磐安县| 丹巴县| 柘荣县| 庆城县| 吉木乃县| 孟津县| 连平县| 隆回县| 鱼台县| 忻城县| 桐庐县| 泸西县| 遂宁市| 霸州市| 贺兰县| 长垣县| 东莞市| 略阳县| 邢台县| 大兴区| 淮滨县| 井冈山市| 津市市| 竹山县| 新建县| 潞城市| 敦煌市| 鄄城县| 邳州市| 聂荣县| 灵璧县| 弥勒县| 万盛区| 益阳市| 黄石市| 广德县| 博客|