java 解压.7z文件与递归删除文件夹

引入maven坐标

<!--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>

    <dependency>
      <artifactId>commons-io</artifactId>
      <groupId>commons-io</groupId>
      <version>2.0.1</version>
    </dependency>
    <!-- slf4j日志工具 -->
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.25</version>
      <scope>compile</scope>
    </dependency>

代码实现:


import org.apache.commons.io.IOUtils; (IOUtils.write使用这个) 

/**
     *
     * @Description (解压7z)
     * @param file7zPath(7z文件路径)
     * @param outPutPath(解压路径)
     * @param passWord(文件密码.没有可随便写,或空)
     * @return
     * @throws Exception
     */
    public static int un7z(String file7zPath, final String outPutPath, String passWord) throws Exception {
        IInArchive archive;
        RandomAccessFile randomAccessFile;
        randomAccessFile = new RandomAccessFile(file7zPath, "r");
        archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile), passWord);
        int numberOfItems = archive.getNumberOfItems();
        ISimpleInArchive simpleInArchive = archive.getSimpleInterface();
        for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
            final int[] hash = new int[] { 0 };
            if (!item.isFolder()) {
                ExtractOperationResult result;
                final long[] sizeArray = new long[1];
                result = item.extractSlow(new ISequentialOutStream() {
                    @Override
                    public int write(byte[] data) throws SevenZipException {
                        try {
                            String str = item.getPath();
                            System.out.println(File.separator);
                            str = str.substring(0, str.lastIndexOf(File.separator));
                            File file = new File(outPutPath + File.separator + str + File.separator);
                            if (!file.exists()) {
                                file.mkdirs();
                            }
                            File file1 = new File(outPutPath + File.separator + item.getPath());
                            IOUtils.write(data, new FileOutputStream(file1, true));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        hash[0] ^= Arrays.hashCode(data); // Consume data
                        sizeArray[0] += data.length;
                        return data.length; // Return amount of consumed
                    }
                }, passWord);
                if (result == ExtractOperationResult.OK) {
                    logger.error("解压成功...." + String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath()));
                } else {
                    logger.error("解压失败:密码错误或者其他错误...." + result);
                }
            }
        }
        archive.close();
        randomAccessFile.close();

        return numberOfItems;
    }

    /**
     * 递归删除文件夹
     *
     * @param file
     */
    public static void deleteFile(File file) {
        if (file.exists()) { // 判断文件是否存在
            if (file.isFile()) { // 判断是否是文件
                file.delete(); // 删除文件
            } else if (file.isDirectory()) { // 否则如果它是一个目录
                File[] files = file.listFiles(); // 声明目录下所有的文件 files[];
                for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
                    deleteFile(files[i]); // 把每个文件用这个方法进行迭代
                }
                file.delete(); // 删除文件夹
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值