自己寫的一個 java 解壓 rar zip 帶密碼 檔案

he_yanghui發表於2020-11-20
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();
        }
    }

}

相關文章