每日一个java算法小分享【089设置RAR压缩包密码】

实例说明

    本实例在RARA命令的基础上实现了图形化操作的加密程序。

关键技术

    本实例通过RAR的命令把用户选定的资源文件压缩为RAR压缩包并支持密码设置功能,设置密码以后只有通过合法的密码才能解压这个RAR压缩包。本实例中用到的RAR完整命令格式如下:

C:\\Program Files\\WinRAR\\Rar.exe a -p"password"<rarFile>

参数说明:

①password:要设置的压缩密码

②rarFile:一个RAR压缩文档文件

例如:

C:\\Program Files\\WinRAR\\Rar.exe a -p"mrsoft" -y 资料.rar *.*

这个命令是把当前文件夹中的所有文件压缩成名称为“资料.rar”的压缩文件,同时设置该压缩文件的密码为“mrsoft”

说明

   本实例仅仅是完成了设置RAR压缩包的密码,并没有在解压文件时实现对密码的进行验证的功能,例如压缩文件的密码为“mrsoft”,解压时,当输入“123”时,被压缩的文件依然会被释放到指定文件夹下。

package com.lzw;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.JProgressBar;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;

public class CompressFileWithPassword extends JFrame {
    
    /**
     * 执行压缩任务的西南侧
     * 
     * @author 李钟尉
     */
private final class CompressThread extends Thread {
    public void run() {
        try {
            progressBar.setString(null);// 初始化进度条控件
            progressBar.setValue(0);
            // 获取密码
            String pass1 = String.valueOf(passwordField1.getPassword());
            // 获取确认密码
            String pass2 = String.valueOf(passwordField2.getPassword());
            String passCommand = "";// 设置密码命令字符串
            if (pass1 != null) {
                if (pass1.equals(pass2)) {// 判断两次密码是否相同
                    passCommand = "-p\"" + pass1 + "\" ";// 完成密码命令
                }else{// 如果两次密码不一样则终止当前命令
                    JOptionPane.showMessageDialog(null, "两次输入密码不一致");
                    return;
                }
            }
            // 获取表格控件的数据模型
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            int rowCount = model.getRowCount();// 获取数据模型中表格行数
            StringBuilder fileList = new StringBuilder();
            for (int i = 0; i < rowCount; i++) {// 遍历数据表格模型中的文件对象
                File file = (File) model.getValueAt(i, 2);
                fileList.append(file.getPath() + "\n");// 把文件路径存到字符串构建器中
            }
            // 创建临时文件,用于保存压缩文件列表
            File listFile = File.createTempFile("fileList", ".tmp");
            FileOutputStream fout = new FileOutputStream(listFile);
            fout.write(fileList.toString().getBytes());// 保存字符串构建器数据到临时文件
            fout.close();
            
            // 创建压缩命令字符串
            final String command = "C:\\Program Files\\WinRAR\\Rar.exe a " + passCommand
                    + rarFile.getPath()  + " @" + listFile.getPath();
            Runtime runtime = Runtime.getRuntime();// 获取Runtime对象
            progress = runtime.exec(command.toString() + "\n");// 执行压缩命令
            progress.getOutputStream().close();// 关闭进程输出流
            // 获取进程输入流
            Scanner scan = new Scanner(progress.getInputStream());
            while (scan.hasNext()) {
                String line = scan.nextLine();// 获取进程提示单行信息
                // 获取提示信息的进度百分比的索引位置
                int index = line.lastIndexOf("%") - 3;
                if (index <= 0)
                    continue;
                // 获取进度百分比字符串
                String substring = line.substring(index, index + 3);
                // 获取整数的百分比数值
                int percent = Integer.parseInt(substring.trim());
                progressBar.setValue(percent);// 在进度条控件显示百分比
            }
            progressBar.setString("完成");
            scan.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    
    private JPanel contentPane;
    private JTable table;
    private JPanel panel;
    private JButton addButton;
    private JButton removeButton;
    private JPanel panel_1;
    private JLabel label;
    private JTextField compressFileField;
    private JButton browseButton;
    private File rarFile;
    private JButton compressButton;
    private JProgressBar progressBar;
    private JButton stopButton;
    private Process progress;
    private JLabel label_1;
    private JLabel label_2;
    private JLabel label_3;
    private JPasswordField passwordField1;
    private JPasswordField passwordField2;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CompressFileWithPassword frame = new CompressFileWithPassword();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public CompressFileWithPassword() {
        setTitle("\u6587\u4EF6\u538B\u7F29\u4E3ARAR\u6587\u6863");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 332);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        
        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);
        
        table = new JTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
                "\u6587\u4EF6\u540D\u79F0", "\u6587\u4EF6\u5927\u5C0F",
                "\u6587\u4EF6\u8DEF\u5F84" }));
        table.getColumnModel().getColumn(0).setPreferredWidth(125);
        table.getColumnModel().getColumn(2).setPreferredWidth(250);
        table.getTableHeader().setReorderingAllowed(false);
        scrollPane.setViewportView(table);
        scrollPane.getViewport().setBackground(Color.WHITE);
        
        panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.SOUTH);
        GridBagLayout gbl_panel_1 = new GridBagLayout();
        gbl_panel_1.columnWidths = new int[] { 0, 60, 0, 0 };
        gbl_panel_1.rowHeights = new int[] { 0, 0, 0, 0, 0 };
        gbl_panel_1.columnWeights = new double[] { 0.0, 1.0, 0.0,
                Double.MIN_VALUE };
        gbl_panel_1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
                Double.MIN_VALUE };
        panel_1.setLayout(gbl_panel_1);
        
        progressBar = new JProgressBar();
        progressBar.setStringPainted(true);
        GridBagConstraints gbc_progressBar = new GridBagConstraints();
        gbc_progressBar.gridwidth = 3;
        gbc_progressBar.insets = new Insets(0, 0, 5, 0);
        gbc_progressBar.fill = GridBagConstraints.HORIZONTAL;
        gbc_progressBar.gridx = 0;
        gbc_progressBar.gridy = 0;
        panel_1.add(progressBar, gbc_progressBar);
        
        label = new JLabel("\u538B\u7F29\u6587\u6863\uFF1A");
        GridBagConstraints gbc_label = new GridBagConstraints();
        gbc_label.fill = GridBagConstraints.HORIZONTAL;
        gbc_label.insets = new Insets(0, 0, 5, 5);
        gbc_label.gridx = 0;
        gbc_label.gridy = 1;
        panel_1.add(label, gbc_label);
        
        compressFileField = new JTextField();
        compressFileField.setEditable(false);
        GridBagConstraints gbc_compressFileField = new GridBagConstraints();
        gbc_compressFileField.insets = new Insets(0, 0, 5, 5);
        gbc_compressFileField.fill = GridBagConstraints.HORIZONTAL;
        gbc_compressFileField.gridx = 1;
        gbc_compressFileField.gridy = 1;
        panel_1.add(compressFileField, gbc_compressFileField);
        compressFileField.setColumns(10);
        
        browseButton = new JButton("\u6D4F\u89C8");
        browseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_browseButton_actionPerformed(arg0);
            }
        });
        GridBagConstraints gbc_browseButton = new GridBagConstraints();
        gbc_browseButton.insets = new Insets(0, 0, 5, 0);
        gbc_browseButton.gridx = 2;
        gbc_browseButton.gridy = 1;
        panel_1.add(browseButton, gbc_browseButton);
        
        label_2 = new JLabel("\u8F93\u5165\u5BC6\u7801\uFF1A");
        GridBagConstraints gbc_label_2 = new GridBagConstraints();
        gbc_label_2.anchor = GridBagConstraints.EAST;
        gbc_label_2.insets = new Insets(0, 0, 5, 5);
        gbc_label_2.gridx = 0;
        gbc_label_2.gridy = 2;
        panel_1.add(label_2, gbc_label_2);
        
        passwordField1 = new JPasswordField();
        passwordField1.setEchoChar('★');
        GridBagConstraints gbc_passwordField1 = new GridBagConstraints();
        gbc_passwordField1.insets = new Insets(0, 0, 5, 5);
        gbc_passwordField1.fill = GridBagConstraints.HORIZONTAL;
        gbc_passwordField1.gridx = 1;
        gbc_passwordField1.gridy = 2;
        panel_1.add(passwordField1, gbc_passwordField1);
        
        label_3 = new JLabel("");
        label_3.setIcon(new ImageIcon(CompressFileWithPassword.class
                .getResource("/com/lzw/key.png")));
        GridBagConstraints gbc_label_3 = new GridBagConstraints();
        gbc_label_3.gridheight = 2;
        gbc_label_3.insets = new Insets(0, 0, 5, 0);
        gbc_label_3.gridx = 2;
        gbc_label_3.gridy = 2;
        panel_1.add(label_3, gbc_label_3);
        
        label_1 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
        GridBagConstraints gbc_label_1 = new GridBagConstraints();
        gbc_label_1.anchor = GridBagConstraints.EAST;
        gbc_label_1.insets = new Insets(0, 0, 0, 5);
        gbc_label_1.gridx = 0;
        gbc_label_1.gridy = 3;
        panel_1.add(label_1, gbc_label_1);
        
        passwordField2 = new JPasswordField();
        passwordField2.setEchoChar('★');
        GridBagConstraints gbc_passwordField2 = new GridBagConstraints();
        gbc_passwordField2.insets = new Insets(0, 0, 0, 5);
        gbc_passwordField2.fill = GridBagConstraints.HORIZONTAL;
        gbc_passwordField2.gridx = 1;
        gbc_passwordField2.gridy = 3;
        panel_1.add(passwordField2, gbc_passwordField2);
        
        panel = new JPanel();
        contentPane.add(panel, BorderLayout.WEST);
        panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        
        addButton = new JButton("\u589E\u52A0");
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_addButton_actionPerformed(arg0);
            }
        });
        panel.add(addButton);
        
        removeButton = new JButton("\u79FB\u9664");
        removeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_removeButton_actionPerformed(arg0);
            }
        });
        panel.add(removeButton);
        
        compressButton = new JButton("\u538B\u7F29");
        compressButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_compressButton_actionPerformed(arg0);
            }
        });
        panel.add(compressButton);
        
        stopButton = new JButton("\u505C\u6B62");
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_stopButton_actionPerformed(arg0);
            }
        });
        panel.add(stopButton);
    }
    
    /**
     * 增加按钮的事件处理方法
     * 
     * @param arg0
     */
    protected void do_addButton_actionPerformed(ActionEvent arg0) {
        JFileChooser chooser = new JFileChooser();// 创建文件选择器
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setMultiSelectionEnabled(true);// 设置允许文件多选
        int option = chooser.showOpenDialog(this);// 显示文件打开对话框
        if (option != JFileChooser.APPROVE_OPTION)
            return;
        File[] files = chooser.getSelectedFiles();// 获取用户选择文件数组
        // 获取表格控件的数据模型
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        for (File file : files) {// 遍历用户选择的文件数组
            // 把文件信息添加到表格控件的模型中
            model.addRow(new Object[] { file.getName(), file.length(), file });
        }
    }
    
    /**
     * 移除按钮的事件处理方法
     * 
     * @param arg0
     */
    protected void do_removeButton_actionPerformed(ActionEvent arg0) {
        int[] rows = table.getSelectedRows();// 获取表格中选中的行索引的数组
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        for (int i = rows.length - 1; i >= 0; i--) {
            model.removeRow(rows[i]);// 遍历并移除所有选中行
        }
    }
    
    /**
     * 选择压缩RAR文档的浏览按钮的事件处理方法
     * 
     * @param arg0
     */
    protected void do_browseButton_actionPerformed(ActionEvent arg0) {
        JFileChooser chooser = new JFileChooser();// 创建文件选择器
        // 设置选择文件类型为Rar
        chooser.setFileFilter(new FileNameExtensionFilter("RAR压缩文档", "rar"));
        chooser.setAcceptAllFileFilterUsed(false);
        int option = chooser.showSaveDialog(this);// 显示保存对话框
        if (option != JFileChooser.APPROVE_OPTION)
            return;
        rarFile = chooser.getSelectedFile();// 获取用户定制的RAR文件
        compressFileField.setText(rarFile.getPath());// 显示RAR文件路径信息
    }
    
    /**
     * 压缩按钮的事件处理方法
     * 
     * @param arg0
     */
    protected void do_compressButton_actionPerformed(ActionEvent arg0) {
        if (rarFile == null) {
            browseButton.doClick();
            if (rarFile == null)
                return;
        }
        progressBar.setVisible(true);
        CompressThread compressThread = new CompressThread(); // 创建压缩线程
        compressThread.start();// 启动线程
    }
    
    /**
     * 停止按钮的事件处理方法
     * 
     * @param arg0
     */
    protected void do_stopButton_actionPerformed(ActionEvent arg0) {
        if (progress != null) {
            progress.destroy();
            progressBar.setValue(0);
            progressBar.setVisible(false);
        }
    }
}

key.png

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 1.53表示这个rar压缩包或状态文件的版本号。该文件未被密码保护,即任何人都可以打开它而不需要输入密码RAR是一种常用的归档文件格式,可以将多个文件压缩成一个文件,减小文件占用空间,并方便传输和存储。RAR压缩包还可以设置密码保护,防止未授权的访问和窃取文件信息。某些情况下,文件未被密码保护可以带来方便,例如在共享文件时可以直接打开,无需输入密码;但同时也会带来安全隐患,如果存储敏感信息,就有可能被他人窃取和滥用。因此,应根据具体情况进行设置。如果要设置密码保护,可以在压缩包创建或解压时进行设置。 ### 回答2: arpr 1.53 文件是一个未被密码保护的 rar 压缩包或状态文件。这意味着该文件可以被任何人解压和访问其中的内容,而不需要提供密码或其他验证信息。如果您拥有该文件,那么您应该确保其内容的安全性和机密性,特别是如果其中包含敏感信息。建议您在将文件共享给其他人或上传到云存储时进行加密或其他安全措施,以保护文件中的数据不被未经授权的人访问。同时,您应该选择合适的压缩工具和加密算法,以确保文件不会被黑客或其他恶意用户攻击或盗取。如果您对如何保护敏感信息的最佳实践有疑问,可以咨询相关的安全专家或在线资源,以获得更多的建议和指导。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值