首页>代码>springboot实现超级好用的windows+Linux可视化文件操作项目>/spring-boot-filemanager/src/main/java/org/shaofan/utils/RarUtils.java
/*
 * Copyright (c) 2014 www.diligrp.com All rights reserved.
 * 本软件源代码版权归地利集团所有,未经许可不得任意复制与传播.
 */
package org.shaofan.utils;

import com.github.junrar.Archive;
import com.github.junrar.exception.RarException;
import com.github.junrar.rarfile.FileHeader;

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

/**
 * RarUtils on spring-boot-filemanager
 *
 * @author <a href="mailto:akhuting@hotmail.com">Alex Yang</a>
 * @date 2016年08月25日 10:13
 */
public class RarUtils {

    public static void unRarFile(File srcRar, String dstDirectoryPath) throws IOException, RarException {

        Archive archive = new Archive(srcRar);
        FileHeader fh = archive.nextFileHeader();
        while (fh != null) {
            String path = fh.getFileNameString().replaceAll("\\\\", "/");
            File dirFile = new File(dstDirectoryPath + File.separator
                    + path);
            if (fh.isDirectory()) { // 文件夹
                dirFile.mkdirs();
            } else { // 文件
                try {// 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
                    if (!dirFile.exists()) {
                        if (!dirFile.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.
                            dirFile.getParentFile().mkdirs();
                        }
                        dirFile.createNewFile();
                    }
                    FileOutputStream os = new FileOutputStream(dirFile);
                    archive.extractFile(fh, os);
                    os.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            fh = archive.nextFileHeader();
        }
        archive.close();

    }
}
最近下载更多
WBelong  LV7 2023年12月28日
jinandfei  LV12 2023年12月27日
yang1115  LV2 2023年4月27日
139465  LV12 2023年3月29日
加油干阳神  LV8 2022年5月8日
crosa_Don  LV18 2022年4月1日
xiex909  LV27 2022年3月30日
回音哥  LV13 2022年2月15日
423256321  LV4 2021年12月27日
Tiam123  LV3 2021年12月22日
最近浏览更多
genyuan2014  LV6 5月7日
fff2003  LV6 4月12日
heweimin  LV12 3月8日
小王wang  LV10 2月29日
氟西汀来救你  LV2 1月9日
WBelong  LV7 2023年12月28日
denglu123321  LV4 2023年12月27日
jinandfei  LV12 2023年12月27日
rain112  LV30 2023年11月29日
lichengai  LV7 2023年11月27日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友