java解压7z格式的压缩包

本文介绍了如何在Java中使用Apache Commons Compress库来解压7z格式的压缩包,特别是在遇到Windows上能成功解压但在Linux服务器上失败的问题时,提供了解决方案。通过引入相关jar包并调用特定方法,可以在Linux环境中正确解压7z文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我的前面博客中总结了一个压缩,解压缩的工具类,http://blog.youkuaiyun.com/u010248330/article/details/74178100。但是针对.7z格式的压缩包,我们用的这两个开源包:

<dependency>
            <groupId>net.sf.sevenzipjbinding</groupId>
            <artifactId>sevenzipjbinding</artifactId>
            <version>9.20-2.00beta</version>
        </dependency>
        <dependency>
            <groupId>net.sf.sevenzipjbinding</groupId>
            <artifactId>sevenzipjbinding-all-platforms</artifactId>
            <version>9.20-2.00beta</version>
</dependency>

在我自己的本机window上是可以成功解压.7z格式的,但是放在我们的linux服务器报错了,而且更严重是这种错误直接就把我们服务器上的tomcat给关了。。。很是郁闷,在网上找了一下,也没找到怎么处理这个。
先把报错贴一下:
这里写图片描述

这里写图片描述

所以不得已找其他的7z的解压方法,在apache的common-compress提供了解压缩7z的方法。
请参考:http://commons.apache.org/proper/commons-compress/examples.html

在maven项目中引入下面两个jar包,否则自己下载下面这两个jar包放到自己的java项目中即可。

 <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-compress</artifactId>
        <version>1.9</version>
</dependency>
<dependency>
      <groupId>org.tukaani</groupId>
      <artifactId>xz</artifactId>
      <version>1.5</version>
</dependency>

代码:

    /**
     * @author kxl
     * @param orgPath 源压缩文件地址
     * @param tarpath 解压后存放的目录地址
     */
    public static void apache7ZDecomp(String orgPath, String tarpath) {

        try {
            SevenZFile sevenZFile = new SevenZFile(new File(orgPath));
            SevenZArchiveEntry entry = sevenZFile.getNextEntry();
            while (entry != null) {

                // System.out.println(entry.getName());
                if (entry.isDirectory()) {

                    new File(tarpath + entry.getName()).mkdirs();
                    entry = sevenZFile.getNextEntry();
                    continue;
                }
                FileOutputStream out = new FileOutputStream(tarpath
                        + File.separator + entry.getName());
                byte[] content = new byte[(int) entry.getSize()];
                sevenZFile.read(content, 0, content.length);
                out.write(content);
                out.close();
                entry = sevenZFile.getNextEntry();
            }
            sevenZFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

调用apache7ZDecomp(“C:\Users\Administrator\Desktop\图片\图片.7z”,”C:\Users\Administrator\Desktop\图片\”);

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值