自己写的一个 java 解压 rar zip 带密码 文件

package com.example.demo.util;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hyh
 */
public class UnZipRarUtil {



    /**
     * @param source   原始文件路径
     * @param dest     解压路径
     * @param password 解压文件密码(可以为空)
     */
    public static int unZip(String source, String dest, String password) {
        try {
            File zipFile = new File(source);
            ZipFile zFile = new ZipFile(zipFile);
            zFile.setFileNameCharset("GBK");
            File destDir = new File(dest);
            if (!destDir.exists()) {
                destDir.mkdirs();
            }
            if (zFile.isEncrypted()) {
                zFile.setPassword(password);
            }
            // 将文件抽出到解压目录(解压)
            zFile.extractAll(dest);
            List<FileHeader> headerList = zFile.getFileHeaders();
            List<File> extractedFileList = new ArrayList();
            for (FileHeader fileHeader : headerList) {
                if (!fileHeader.isDirectory()) {
                    extractedFileList.add(new File(destDir, fileHeader.getFileName()));
                }
            }
            File[] extractedFiles = new File[extractedFileList.size()];
            extractedFileList.toArray(extractedFiles);
            for (File f : extractedFileList) {
                System.out.println(f.getAbsolutePath() + "文件解压成功!");
            }
        } catch (ZipException e) {
            e.printStackTrace();
            return 0;
        }
        System.out.println("解压后的密码:" + password);
        return 1;
    }


    public static void findpwd() {
        String[] dic ={"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l",
                "z","x","c","v","b","n","m","Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X",
                "C","V","B","N","M","1","2","3","4","5","6","7","8","9","0","`","!","@","#","$","%","&","*","(",")","-","_","+","=","[",
                "]","{","}","|","\\","\"",":",";","\'","<",">",",",".","?","/"};
    }

    /**
     *  指定长度 指定密码范围
     * @param passwords 密码范围   如 "123456abcdefg"
     * @param pLength  密码长度
     * @param getPass 密码
     */
    public static String makePassWord(String passwords, int pLength, char[] getPass) {
        char[] passChars = passwords.toCharArray();

        if(pLength == 0){
            String password = new String(getPass).trim();
            System.out.println(password);
            return password;
        }
        for (int i = 0; i < passChars.length; i++) {
            getPass[pLength] = passChars[i];
            makePassWord( passwords, pLength-1, getPass);
        }
        return null;
    }

    /**
     *   小于等于密码指定长度所有组合(如 长度为3  则取 长度为1 2 3 的密码) 指定范围
     * @param passwords
     * @param pLength
     */
    public static String makePassWord2(String passwords, int pLength) {
        for (int i = 1; i <= pLength; i++) {
            return makePassWord(passwords, i,new char[i+1]);
        }
        return null;
    }

    /**
     *  密码 List
     * @param passwords
     * @param pLength
     * @return
     */
    public static List<String> pwList(String passwords, int pLength) {
        List<String> list = new ArrayList<>();
        String s = makePassWord2(passwords, pLength);
        list.add(s);
        return list;
    }

    /**
     *  破解 Rar
     * @param passwords
     * @param pLength
     */
    public static void pjRar(String passwords, int pLength,String filePath,String targetPath){
        List<String> pwList = pwList(passwords, pLength);
        pwList.forEach(pw ->{
            unRar( filePath, targetPath, pw);
        });
    }
    /**
     *  破解 Rar
     * @param passwords
     * @param pLength
     */
    public static void pjZip(String passwords, int pLength,String filePath,String targetPath){
        List<String> pwList = pwList(passwords, pLength);
        for (String pw:pwList){
            int i = unZip(filePath, targetPath, pw);
            if(i == 1){
                return;
            }
        }
    }


    /**
     *  解压 rar 文件
     * @param filePath
     * @param targetPath
     * @param password
     * @throws Exception
     */
    public static void unRar(String filePath,String targetPath,String password) {
        //winrar的执行路径
        String rarPath=" D:\\360\\WinRAR\\Rar.exe";
        StringBuilder sb = new StringBuilder();
        sb.append(rarPath);
        sb.append(" x -ibck -hp");
        sb.append(password).append(" -y ").append(filePath+" "+targetPath);
        Process process;
        try {
            process = Runtime.getRuntime().exec(sb.toString());
            if(process.waitFor() ==0 ){
                // 将密码写出来
                FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\password.txt"));
                String s = "--成功:解压后的密码:"+password;
                fileOutputStream.write(s.getBytes());
                fileOutputStream.flush();
                fileOutputStream.close();
                System.out.println(s);
                return;
            }else {
                System.out.println(new Date()+"--失败:"+password);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值