Java里解压zip和rar包

news/2024/7/6 3:07:52 标签: java, 解压缩, zip, rar

zip的解压提供了一种方法,
rar的解压提供了两种方法,第一种方法是调用命令调用主机安装的解压缩工具,
第二种方法,需要注意一下,需要导一个包

<dependency>
    <groupId>com.github.junrar</groupId>
    <artifactId>junrar</artifactId>
    <version>4.0.0</version>
</dependency>

并且第二种方法,截至2023年,只支持rar4以下版本的解压,rar5的版本不支持,以后会不会有更新,就不知道了。
在这里插入图片描述

java">try{
                File file = new File(filePath);
                if(file.getName().endsWith(".zip")){
                        ZipInputStream zis = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK"));
                        byte[] buffer = new byte[1024];
                        ZipEntry ze;
                        while ((ze = zis.getNextEntry()) != null) {
                            String entryName = ze.getName();
                            File extractedFile = new File(ceShiFilePath + entryName);
                            if (ze.isDirectory()) {
                                extractedFile.mkdirs();
                            } else{
                                FileOutputStream fos = new FileOutputStream(extractedFile);
                                int len;
                                while ((len = zis.read(buffer)) > 0) {
                                    fos.write(buffer, 0, len);
                                }
                                fos.close();
                                fileList.add(extractedFile);
                            }
                        }
                        zis.closeEntry();
                }else if(file.getName().endsWith(".rar")){
                    String cmd = "D:\\tools\\zip\\UnRAR.exe";
                    //cmd命令
                    String unrarCmd = cmd + " x -r -p- -o+ " + file.getAbsolutePath() + " "
                            + ceShiFilePath;
                    Runtime rt = Runtime.getRuntime();
                    //调用程序UnRAR解压程序(Windows)
                    Process pre = rt.exec(unrarCmd);
                    //防止文件乱码
                    InputStreamReader isr = new InputStreamReader(pre.getInputStream(),"GBK");
                    BufferedReader bf = new BufferedReader(isr);
                    String line = null;
                    while ((line = bf.readLine()) != null) {
                        line = line.trim();
                        if ("".equals(line)) {
                            continue;
                        }
                    }
                    bf.close();
                    pre.destroy();
                }else if(file.getName().endsWith(".rar")){
                    Archive archive = new Archive(new FileInputStream(file));
                    List<FileHeader> fileHeaders = archive.getFileHeaders();
                    for (FileHeader fileHeader : fileHeaders) {
                        if (fileHeader.isDirectory()) {
                            File dir = new File(ceShiFilePath + fileHeader.getFileNameString());
                            if (!dir.exists()){
                                dir.mkdirs();
                            }
                        } else {
                            String fileName=  fileHeader.getFileNameW().trim();
                            File subFile = new File(ceShiFilePath + fileName);
                            if (!subFile.exists()) {
                                if (!subFile.getParentFile().exists()) {
                                    subFile.getParentFile().mkdirs();
                                }
                                subFile.createNewFile();
                            }
                            FileOutputStream os = new FileOutputStream(subFile);
                            archive.extractFile(fileHeader, os);
                            os.close();
                            fileList.add(subFile);
                        }
                    }
                    archive.close();
                }
            for(File file111 : fileList){
                System.out.println(file111.getAbsolutePath());
            }
        }catch (Exception ex){
            ex.printStackTrace();
            log.error(ex.getMessage());
        }

都是亲测可用的。


http://www.niftyadmin.cn/n/5325856.html

相关文章

redis原理(二)数据结构

redis可以存储键与5种不同数据结构类型之间的映射&#xff1a; String类型的底层实现只有一种数据结构&#xff0c;也就是动态字符串。而List、Hash、Set、ZSet都由两种底层数据结构实现。通常我们把这四种类型称为集合类型&#xff0c;它们的特点是一个键对应了一个集合的数据…

CRM管理系统选择技巧-六大步骤助您选择好用的客户管理系统

毫无疑问&#xff0c;一个好的CRM管理系统是任何成长型企业的必备条件。然而&#xff0c;为您的企业选择合适的CRM系统并不容易。打开搜索引擎&#xff0c;有非常多的结果&#xff0c;怎样在数十万个搜索结果中选择适合您的CRM系统&#xff1f;CRM选型要按照明确自身需求、决定…

深入类加载机制及底层

深入类加载机制 初识类加载过程 使用某个类时&#xff0c;如果该类的class文件没有加载到内存时&#xff0c;则系统会通过以下三个步骤来对该类进行初始化 1.类的加载&#xff08;Load&#xff09; → 2.类的连接&#xff08;Link&#xff09; → 3.类的初始化&#xff08;In…

MySQL面试题2

文章目录 面试题 (9-15) 面试题 (9-15) 09&#xff09;查询学过「张三」老师授课的同学的信息 SELECT s.*,c.cname,t.tname FROM t_mysql_teacher t,t_mysql_student s,t_mysql_course c,t_mysql_score sc WHERE t.tidc.tid and c.cidsc.cid and sc.sids.sid and tname ‘张…

LLM之RAG实战(十五)| RAG的自动源引文验证技术

​ 在过去的一年里&#xff0c;检索增强生成&#xff08;RAG&#xff09;已经成为一种基于LLM的流行架构&#xff0c;旨在解决在基于知识的LLM最常见的挑战之一&#xff0c;可怕的幻觉。 一、RAG如何解决幻觉&#xff1f; RAG Pipeline包括两个关键组件&#xff1a;&…

openssl3.2 - 官方demo学习 - digest - EVP_MD_stdin.c

文章目录 openssl3.2 - 官方demo学习 - digest - EVP_MD_stdin.c概述笔记END openssl3.2 - 官方demo学习 - digest - EVP_MD_stdin.c 概述 使用 SHA3-512 对stdin输入做摘要, 并输出摘要值. 笔记 /*! \file EVP_MD_stdin.c \note openssl3.2 - 官方demo学习 - digest - EVP…

【sqlite3】sqlite3在linux下使用sqlitebrowser工具实现数据可视化

sqlite3在linux下使用sqlitebrowser工具实现数据可视化 1. ### install sqlitebrowser 1. ### install sqlitebrowser 安装指令 sudo apt-get install sqlitebrowser通过工具打开数据库 sqlitebrowser stereo.db打开效果

TorchDynamo 原理和示例

TorchDynamo 是一个设计用于加速未修改的 PyTorch 程序的 Python 级即时&#xff08;JIT&#xff09;编译器。它通过 Python Frame Evaluation Hooks&#xff08;Python 框架评估钩子&#xff09;来实现这一目标&#xff0c;以便在运行时动态地生成和优化代码。这使得 TorchDyn…