ProcessBuilder builder = new ProcessBuilder("cmd.exe");
builder.redirectErrorStream(true);
Process process = null;
try {
process = builder.start();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
BufferedReader br = null;
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(),"GBK"));
br = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
try {
String s;
bw.write("bz.exe x -o:"+out_path_label.getText()+" "+file_path_label.getText()+"\n");
bw.flush();
while (!(s = br.readLine()).equals("")){
System.out.println(s);
jta.append(s+"\n");
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
BufferedWriter和BufferedReader指定编码为GBK
BufferedWriter输出命令“bz.exe x -o:E:\【1】应用软件集\chatgpt\ E:\【1】应用软件集\chatgpt\mima.zip”
BufferedReader此时无法读出数据
控制台信息如下:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:F:\java\idea\IntelliJ IDEA Community Edition 2022.3.2\lib\idea_rt.jar=7375:F:\java\idea\IntelliJ IDEA Community Edition 2022.3.2\bin" -Dfile.encoding=UTF-8 -classpath "F:\java\projects\Zip Crack\out\production\Zip Crack" GUI
Microsoft Windows [版本 10.0.19045.2006]
(c) Microsoft Corporation。保留所有权利。
F:\java\projects\Zip Crack>bz.exe x -o:E:\【1】应用软件集\chatgpt\ E:\【1】应用软件集\chatgpt\mima.zip
然而同样的命令正常用cmd执行是这样的:
怎么办?
结尾附上完整源码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class GUI extends JFrame {
public GUI(){
try {
UIManager.setLookAndFeel(javax.swing.plaf.nimbus.NimbusLookAndFeel.class.getName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
UnsupportedLookAndFeelException e) {
throw new RuntimeException(e);
}
Container co = getContentPane();
setLayout(new GridLayout(5,1));
JPanel file_path_panel = new JPanel();
JLabel file_path_label = new JLabel("压缩包路径");
JButton file_path_set_button = new JButton("打开...");
JPanel out_path_panel = new JPanel();
JPanel two_buttons_panel = new JPanel(new GridLayout(2,1));
JLabel out_path_label = new JLabel("导出路径");
JButton out_path_button = new JButton("解压到...");
JButton out_path_this_button = new JButton("解压到当前文件夹");
JTextArea jta = new JTextArea();
jta.setLineWrap(true);
JButton start_crack = new JButton("开始");
JPanel crack_mode = new JPanel(new GridLayout(1,7));
JCheckBox is_number = new JCheckBox("数字");
JCheckBox is_alphabet = new JCheckBox("小写字母");
JCheckBox is_alohabet_toUpperCase = new JCheckBox("大写字母");
JCheckBox is_symbol = new JCheckBox("特殊字符");
JTextField key_start_number = new JTextField();
key_start_number.addKeyListener(new ControlTheInput());
JLabel bolang = new JLabel("~");
JTextField key_end_number = new JTextField();
key_end_number.addKeyListener(new ControlTheInput());
start_crack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
ProcessBuilder builder = new ProcessBuilder("cmd.exe");
builder.redirectErrorStream(true);
Process process = null;
try {
process = builder.start();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
BufferedReader br = null;
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(),"GBK"));
br = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
try {
String s;
bw.write("bz.exe x -o:"+out_path_label.getText()+" "+file_path_label.getText()+"\n");
bw.flush();
while (!(s = br.readLine()).equals("")){
System.out.println(s);
jta.append(s+"\n");
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
KeyDictionary kd = new KeyDictionary();
for(int i = Integer.parseInt(key_start_number.getText());i <= Integer.parseInt(key_end_number.getText());i++){
kd.init(is_number.isSelected(),is_alphabet.isSelected(),is_alohabet_toUpperCase.isSelected(),is_symbol.isSelected(),i);
String end_key = kd.get_end_key();
while (true){
String key = kd.GetKey()+"\n";
try {
bw.write(key);
bw.flush();
String c = null;
if (!read(c,jta,br)){
break;
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
if (key.equals(end_key)){
break;
}
}
}
try {
br.close();
bw.close();
process.destroy();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}).start();
}
});
file_path_set_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser file_path_chooser = new JFileChooser();
file_path_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int i = file_path_chooser.showOpenDialog(getContentPane());
if(i == JFileChooser.APPROVE_OPTION) {
File zip_file = file_path_chooser.getSelectedFile();
file_path_label.setText(zip_file.getAbsolutePath());
}
}
});
out_path_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser out_path_chooser = new JFileChooser();
out_path_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int i = out_path_chooser.showOpenDialog(getContentPane());
if(i == JFileChooser.APPROVE_OPTION) {
File out_path = out_path_chooser.getSelectedFile();
out_path_label.setText(out_path.getAbsolutePath());
}
}
});
out_path_this_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String[] split = file_path_label.getText().split("\\\\");
String new_file_path = file_path_label.getText().replace(split[split.length-1],"");
out_path_label.setText(new_file_path);
}
});
co.add(jta);
file_path_panel.add(file_path_label,BorderLayout.CENTER);
file_path_panel.add(file_path_set_button,BorderLayout.EAST);
co.add(file_path_panel);
two_buttons_panel.add(out_path_button);
two_buttons_panel.add(out_path_this_button);
out_path_panel.add(out_path_label);
out_path_panel.add(two_buttons_panel);
co.add(out_path_panel);
crack_mode.add(is_number);
crack_mode.add(is_alphabet);
crack_mode.add(is_alohabet_toUpperCase);
crack_mode.add(is_symbol);
crack_mode.add(key_start_number);
crack_mode.add(bolang);
crack_mode.add(key_end_number);
co.add(crack_mode);
co.add(start_crack);
setBounds(600,300,600,600);
setVisible(true);
setTitle("Zip Crack");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private boolean read(String c,JTextArea jta,BufferedReader br) {
while (true){
try {
if ((c = br.readLine()) == null) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
jta.append(c+"\n");
System.out.println(c);
if (c.equals("Invalid password")){
return true;
}
}
return false;
}
class ControlTheInput extends KeyAdapter {
public void keyTyped(KeyEvent e) {
String key="0123456789"+(char)8;
if(key.indexOf(e.getKeyChar())<0){
e.consume();//如果不是数字则取消
}
}
}
public static void main(String[] args) {
new GUI();
}
}