Java解压zip与rar4

这篇文章介绍了一个Java工具类DecompressionUtil,用于解压zip和RAR格式的压缩包,利用ApacheCommonsCompress和junrar库进行操作,支持RAR4格式但不支持RAR5。
摘要由CSDN通过智能技术生成
import com.github.junrar.Archive;
import com.github.junrar.exception.RarException;
import com.github.junrar.rarfile.FileHeader;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;

import java.io.*;

public class DecompressionUtil {

    /**
     *
     * @param filePath 压缩包路径
     * @param destDirectory 解压后的文件所在目录
     */
    public static void decompression(String filePath, String destDirectory){
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        try{
            if(filePath.endsWith("zip")){
                unzip(filePath,destDirectory);
            }else if(filePath.endsWith("rar")){
                unrar(filePath,destDirectory);
            }else {
                System.out.println("暂时不持支该格式的压缩包");
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void unzip(String zipFilePath, String destDirectory) throws IOException {
        try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new FileInputStream(zipFilePath))) {
            ZipArchiveEntry entry;

            while ((entry = zipInputStream.getNextZipEntry()) != null) {
                String filePath = destDirectory + File.separator + entry.getName();

                if (!entry.isDirectory()) {
                    extractFile(zipInputStream, filePath);
                } else {
                    new File(filePath).mkdir();
                }
            }
        }
    }

    private static void unrar(String rarFilePath, String destDirectory) throws IOException, ArchiveException {
        try {
            Archive archive = new Archive(new File(rarFilePath));

            // 遍历RAR文件中的项
            FileHeader fileHeader;
            while ((fileHeader = archive.nextFileHeader()) != null) {
                // 构建解压缩的目标文件路径
                String destFilePath = destDirectory+ fileHeader.getFileName();
                if(fileHeader.getFileName()==null||"".equals(fileHeader.getFileName())){
                    continue;
                }
                // 创建解压缩的目标文件
                File destFile = new File(destFilePath);
                if(destFile.exists()){
                    continue;
                }
                destFile.getParentFile().mkdirs();

                // 解压缩RAR文件中的项
                try (FileOutputStream fos = new FileOutputStream(destFile)) {
                    archive.extractFile(fileHeader, fos);
                }
            }


        } catch (IOException | RarException e) {
            e.printStackTrace();
        }
        System.out.println("解压缩完成!");
    }



    private static void extractFile(InputStream inputStream, String filePath) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(filePath)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                fos.write(buffer, 0, length);
            }
        }
    }
}

核心依赖:

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-compress</artifactId>
      <version>1.18</version>
    </dependency>
    <dependency>
      <groupId>com.github.junrar</groupId>
      <artifactId>junrar</artifactId>
      <version>7.5.5</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-nop</artifactId>
      <version>1.7.2</version>
    </dependency>

WinRAR默认的压缩方式是rar5,上述代码中的解压rar只能解压rar4。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值