java解压7z格式的压缩包

在我的前面博客中总结了一个压缩,解压缩的工具类,http://blog.csdn.net/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\图片\”);

猜你喜欢

转载自blog.csdn.net/u010248330/article/details/75389018
今日推荐