递归解压rar压缩文件

依赖

    com.github.junrar
    junrar
    3.0.0
/**
 * @Author Math
 * @Description 递归解压rar文件
 * @Date  2022/8/26
 * @Version 1.0
 * @Param [file, destDirPath]
 * @Param [要解压的文件, 解压后路径]
 * @return void
 **/
public static void unRar(File file, String destDirPath) throws Exception {
    Archive archive = null;
    File destFileName;
    FileOutputStream fos;
    if (file != null && file.length() > 0) {
        archive = new Archive(new FileVolumeManager(file));
    }
    if (archive == null) {
        throw new Exception("请检查上传rar包版本是否为5.0以下");
    }
    FileHeader fh;
    while ((fh = archive.nextFileHeader()) != null) {
        String fileName = fh.getFileNameW().trim();
        if (!existChinese(fileName)) {
            fileName = fh.getFileNameString().trim();
        }
        fileName = fileName.replaceAll("\\\\", "/");
        destFileName = new File(destDirPath + "/" + fileName);
        if (fh.isDirectory()) {
            continue;
        }
        if (!destFileName.getParentFile().exists()) {
            destFileName.mkdirs();
        }
        fos = new FileOutputStream(destFileName);
        archive.extractFile(fh, fos);
        if (fos != null) {
            fos.close();
        }
        String name = fileName.substring(0, fileName.lastIndexOf("."));
        if (fileName.contains(".rar")) {
            unRar(destFileName, destDirPath + "/" + name);
            destFileName.delete();
            continue;
        }
    }
    if (archive != null) {
        archive.close();
    }
}
/**
 * @Author Math
 * @Description 检测中文
 * @Date  2022/8/26
 * @Version 1.0
 * @Param [str]
 * @return boolean
 **/
public static boolean existChinese(String str) {
    String regEx = "[\\u4e00-\\u9fa5]";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(str);
    while (m.find()) {
        return true;
    }
    return false;
}

你可能感兴趣的:(java,java)