JAVA解压ZIP格式的压缩包

1、需要的引入的jar包

<dependency>

  <groupId>net.lingala.zip4j</groupId>

  <artifactId>zip4j</artifactId>

  <version>1.3.2</version>

</dependency>

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");

    }

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容