解压7z

build.gradle

        // un7z
        compile 'org.apache.commons:commons-compress:1.15'
        compile 'org.tukaani:xz:1.8'
package com.jdsoft.util;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Un7zipUtils {

    /**
     * 解压 7z 格式的压缩文件 (apache的common-compress)
     * @param orgPath 源压缩文件地址
     * @param tarPath 解压后存放的目录地址
     */
    public static void apacheUn7Z(String orgPath, String tarPath) {
        try {
            SevenZFile sevenZFile = new SevenZFile(new File(orgPath));
            SevenZArchiveEntry entry = sevenZFile.getNextEntry();
            while (entry != null) {
                if (entry.isDirectory()) {
                    new File(tarPath + File.separator + 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 (IOException e) {
            e.printStackTrace();
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Gradle对于很多开发者来说有一种既熟悉又陌生的感觉,他是离我们那么近,以至于我每天做项目都需要他,但是他又是离...
    阿_希爸阅读 9,606评论 10 199
  • 题记:在2017年还剩三两天的时刻,朋友圈被十八岁刷屏了,感于时间流逝,胡乱涂鸦此文。再见17,你好18~~~~ ...
    文戈乎乎阅读 347评论 0 0
  • 近些年来,奶奶身体一年不如一年,临行前去和她告别,刚进院子,看见她坐在大厅抱着热水袋发着呆,听见我喊她,满脸慈祥的...
    咸鱼逆流阅读 363评论 0 0
  • 文 | 盛夏28天 我正在参加怦然心动·邂逅你的11封情书——1111情书交友创作大赛,快来给我写情书吧。 昵称:...
    盛夏28天阅读 606评论 13 8
  • 青蛙的生活记 我们要回大陆! 你们这样做会后悔的!
    有趣文叔阅读 231评论 0 0