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;
     }


 /**
      * 删除文件目录及文件
      *
      * @param delFile
      */
     private void deleteFile(File delFile) {

           // 删除单个文件
           if (delFile.exists()) {
                if (delFile.isFile()) {
                     delFile.delete();
                } else if (delFile.isDirectory()) {
                     // 如果是目录 遍历目录下的文件 删除文件后删除目录
                     File[] files = delFile.listFiles();
                     for (int i = 0; i < files.length; i++) {
                           deleteFile(files[i]);
                     }
                     delFile.delete();// 删除掉目录
                }
           }
     }

     /**
      * 删除本地目录及文件
      *
      * @param sPath
      *            绝对路径
      */
     private void deleteFolder(String sPath) {

           File file = new File(sPath);
           // 判断目录或文件是否存在
           if (!file.exists()) { // 不存在返回 false
                return;
           } else {
                // 判断是否为文件
                deleteFile(file);
           }

     }
 

猜你喜欢

转载自javaxiaoyetan.iteye.com/blog/2380139