JAVA解压压缩包(代码实现)

JAVA解压压缩包

zip格式的需要用java.util.zip包,rar格式的需要用com.github.junrar

/** 
      * 解压zip格式的压缩文件到指定位置 
      * 
      * @param zipFileName 
      *            压缩文件 全路径 
      * @param dstDirectoryPath 
      *            解压目录 
      * @throws Exception 
      */  
     public static boolean unZipFile(String zipFileName, String dstDirectoryPath)  
                throws Exception {  
           // 乱码问题以及文件内容重复问题  
           System.setProperty("sun.zip.encoding",  
                     System.getProperty("sun.jnu.encoding"));  
           try {  
                File dstDiretory = new File(dstDirectoryPath);  
                if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹  
                     dstDiretory.mkdirs();  
                }  
                File f = new File(zipFileName);  
                if ((!f.exists()) && (f.length() <= 0)) {  
                     throw new Exception("要解压的文件不存在!");  
                }  
               ZipFile zipFile = new ZipFile(zipFileName, Charset.forName("gbk")); // 处理中文文件名乱码的问题  
                String strPath, gbkPath, strtemp;  
                File tempFile = new File(dstDirectoryPath);  
                strPath = tempFile.getAbsolutePath();  
                Enumeration<?> e = zipFile.entries();  
                while (e.hasMoreElements()) {  
                     ZipEntry zipEnt = (ZipEntry) e.nextElement();  
                     gbkPath = zipEnt.getName();  
                     // 判断是目录还是文件 如果是文件直接解析和上传  
                     // 如果是目录 创建文件夹 递归去处理文件  
                     if (zipEnt.isDirectory()) {  
                           strtemp = strPath + File.separator + gbkPath;  
                           File dir = new File(strtemp);  
                           dir.mkdirs();  
                           continue;  
                     } else {  
                           // 读写文件  
                           InputStream is = zipFile.getInputStream(zipEnt);  
                           BufferedInputStream bis = new BufferedInputStream(is);  
                           gbkPath = zipEnt.getName();  
                           strtemp = strPath + File.separator + gbkPath;  
  
                           // 建目录 直接处理怕出现重名 覆盖  
                           String strsubdir = gbkPath;  
                           for (int i = 0; i < strsubdir.length(); i++) {  
                                if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {  
                                     String temp = strPath + File.separator  
                                                + strsubdir.substring(0, i);  
                                     File subdir = new File(temp);  
                                     if (!subdir.exists())  
                                           subdir.mkdir();  
                                }  
                           }  
                           FileOutputStream fos = new FileOutputStream(strtemp);  
                           BufferedOutputStream bos = new BufferedOutputStream(fos);  
                           int c;  
                           while ((c = bis.read()) != -1) {  
                                bos.write((byte) c);  
                           }  
                           bos.close();  
                           fos.close();  
  
                     }  
                }  
  
                return true;  
           } catch (Exception e) {  
                e.printStackTrace();  
                return false;  
           }  
     }  
  
     /** 
      * 根据原始rar路径,解压到指定文件夹下. 
      * 
      * @param srcRarPath 
      *            原始rar路径 
      * @param dstDirectoryPath 
      *            解压到的文件夹 
      */  
     public static boolean unRarFile(String srcRarPath, String dstDirectoryPath) {  
           File dstDiretory = new File(dstDirectoryPath);  
           if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹  
                dstDiretory.mkdirs();  
           }  
           Archive a = null;  
           File rarFile = null;  
           try {  
                rarFile = new File(srcRarPath);  
                if (!rarFile.exists()) {  
                     return false;  
                } else {  
                     a = new Archive(rarFile);  
                     if (a != null) {  
                           // a.getMainHeader().print(); // 打印文件信息.  
                           FileHeader fh = a.nextFileHeader();  
                           while (fh != null) {  
                                // 防止文件名中文乱码问题的处理  
  
                                String fileName = fh.getFileNameW().isEmpty() ? fh  
                                           .getFileNameString() : fh.getFileNameW();  
                                if (fh.isDirectory()) { // 文件夹  
                                     File fol = new File(dstDirectoryPath  
                                                + File.separator + fileName);  
                                     fol.mkdirs();  
                                } else { // 文件  
                                     File out = new File(dstDirectoryPath  
                                                + File.separator + fileName.trim());  
                                     try {  
                                           if (!out.exists()) {  
                                                if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.  
                                                     out.getParentFile().mkdirs();  
                                                }  
                                                out.createNewFile();  
                                           }  
                                           FileOutputStream os = new FileOutputStream(out);  
                                           a.extractFile(fh, os);  
                                           os.close();  
                                     } catch (Exception ex) {  
                                           ex.printStackTrace();  
                                     }  
                                }  
                                fh = a.nextFileHeader();  
                           }  
                           a.close();  
                     }  
                }  
                return true;  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
  
           return false;  
     }
     ```
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值