java解压缩zip、rar

解压缩zip

  1. 使用hutool工具包中ZipUtil工具类
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.2</version>
</dependency>
// 参数是压缩包路径和编码
// GBK是为了解决中文解压缩乱码的问题
ZipUtil.unzip(f.getAbsolutePath(), Charset.forName("GBK"));
  1. 使用zip4j工具包
<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>2.9.1</version>
</dependency>
ZipFile zipFile = new ZipFile(f.getAbsolutePath());
// 中文乱码处理
zipFile.setCharset(Charset.forName("GBK"));
File zdir = new File(f.getAbsolutePath().substring(0, f.getAbsolutePath().indexOf(".")));
if (!zdir.isDirectory()) {
    zdir.mkdir();
}
try {
	// 解压到指定文件夹
    zipFile.extractAll(zdir.getAbsolutePath());
} catch (ZipException e) {
    e.printStackTrace();
}

解压缩rar

  1. 使用junrar工具包
<dependency>
    <groupId>com.github.junrar</groupId>
    <artifactId>junrar</artifactId>
    <version>7.4.0</version>
</dependency>
File zdir = new File(f.getAbsolutePath().substring(0, f.getAbsolutePath().indexOf(".")));
if (!zdir.isDirectory()) {
    zdir.mkdir();
}
try {
    Junrar.extract(f.getAbsolutePath(), zdir.getAbsolutePath());
} catch (IOException e) {
    e.printStackTrace();
} catch (RarException e) {
    e.printStackTrace();
}
  1. 使用sevenzipjbinding
<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding</artifactId>
    <version>16.02-2.01</version>
</dependency>
<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding-all-platforms</artifactId>
    <version>16.02-2.01</version>
</dependency>
try {
   // f -  压缩文件
   RandomAccessFile randomAccessFile = new RandomAccessFile(f.getAbsolutePath(), "r");
   IInArchive archive = SevenZip.openInArchive(ArchiveFormat.RAR5,  new RandomAccessFileInStream(randomAccessFile));
   // 解压文件路径
   File zdir = new File( f.getAbsolutePath().substring(0,  f.getAbsolutePath() .indexOf(".")));
   if (!zdir.isDirectory()) {
       zdir.mkdir();
   }

   int[] in = new int[archive.getNumberOfItems()];
   for(int i=0;i<in.length;i++){
       in[i] = i;
   }
   archive.extract(in, false, new ExtractCallback(archive, zdir.getAbsolutePath()));
   archive.close();
   randomAccessFile.close();
} catch (FileNotFoundException | SevenZipException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}
private static class ExtractCallback implements IArchiveExtractCallback {
    private final IInArchive inArchive;
    private final String extractPath;
    public ExtractCallback(IInArchive inArchive, String extractPath) {
        this.inArchive = inArchive;
        if (!extractPath.endsWith("/") && !extractPath.endsWith("\\")) {
            extractPath += File.separator;
        }
        this.extractPath = extractPath;
    }

    @Override
    public void setTotal(long total) {

    }

    @Override
    public void setCompleted(long complete) {

    }

    @Override
    public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
        return data -> {
            String filePath = inArchive.getStringProperty(index, PropID.PATH);
            FileOutputStream fos = null;
            try {
                File path = new File(extractPath + filePath);
                if(!path.getParentFile().exists()){
                    path.getParentFile().mkdirs();
                }

        if(!path.exists()){
            path.createNewFile();
        }
        fos = new FileOutputStream(path, true);
        fos.write(data);
            } catch (IOException e) {
        log.info("IOException while extracting " + filePath);
        } finally{
            try {
            if(fos != null){
            fos.flush();
            fos.close();
            }
        } catch (IOException e) {
            log.error("Could not close FileOutputStream", e);
        }
        }
        return data.length;
    };
    }

    @Override
    public void prepareOperation(ExtractAskMode extractAskMode) {

    }

    @Override
    public void setOperationResult(ExtractOperationResult extractOperationResult) {
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讓丄帝愛伱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值