JAVA解压ZIP格式的压缩包

1、需要的引入的jar包

  net.lingala.zip4j

  zip4j

  1.3.2

2、解压ZIP格式的文件

public static void unzip(String srcFile,String  destDirPath) {

         long startTime=System.nanoTime();   //获取开始时间

         try {

             /** 判断文件是否存在 */

             File file = new File(srcFile);

             if (file.exists()) {

                 /** 判断文件是否是zip格式的压缩文件  */

                 // 获取文件的后缀

                 String fileSuffix =  file.getName().substring(file.getName().lastIndexOf("."));

                 if (".zip".equals(fileSuffix)) {

                      net.lingala.zip4j.core.ZipFile zipFile = new  net.lingala.zip4j.core.ZipFile(srcFile);

                      // 设置编码格式中文设置为GBK格式

                      zipFile.setFileNameCharset("GBK");

                      // 解压压缩包

                      zipFile.extractAll(destDirPath);

                 }

             }

         } catch (Exception e) {

             e.printStackTrace();

         }

         long endTime=System.nanoTime(); //获取结束时间  

         System.out.println("程序运行时间:  "+(endTime-startTime)+"ns");

         System.out.println("程序运行时间:  "+(endTime-startTime)/1000000+"ms");

         System.out.println("程序运行时间:  "+(endTime-startTime)/1000000000+"s");

    }

你可能感兴趣的:(JAVA解压ZIP格式的压缩包)