⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jrargui.java

📁 效率较高的文件压缩算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        f.getContentPane().add("Center", tablePanel);
        //this.getContentPane().add("Center", scrollPane);
    }

    private void createStatusBar() {
        jrar
                .addToDebugWindow("Creating the status bar panel and giving it a Box layout");
        statusBar = new JPanel();
        statusBar.setLayout(new BoxLayout(statusBar, BoxLayout.LINE_AXIS));
        jrar.addToDebugWindow("Creating the JLabel for the status bar");
        jl = new JLabel("Finished Loading");
        jrar
                .addToDebugWindow("Adding a small rigid box to the status bar panel");
        statusBar.add(Box.createRigidArea(new Dimension(10, 0)));
        jrar.addToDebugWindow("Adding the JLabel to the statud bar panel");
        statusBar.add(jl);
        jrar
                .addToDebugWindow("Adding the status bar to the South of the main frame");
        f.getContentPane().add("South", statusBar);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == openArchiveButton) {
            jrar
                    .addToDebugWindow("Open arcive button pressed, calling Jrar.openFileChooser()");
            openFileChooser();
        } else if (e.getSource() == addToArchiveButton) {
            jrar
                    .addToDebugWindow("Add button pressed, calling Jrar.addFilesToArchive()");
            addFilesToArchive();
        } else if (e.getSource() == extractToButton) {
            jrar
                    .addToDebugWindow("Extract To button pressed, calling Jrar.extractFilesTo()");
            extractFilesTo();
        } else if (e.getSource() == sfxButton) {
            jrar
                    .addToDebugWindow("SFX button pressed, calling <NOTHING CALLED YET>");
        } else if (e.getSource() == testArchiveButton) {
            jrar
                    .addToDebugWindow("Test Archive button pressed, calling Jrar.testArchive()");
            //      testArchive();
        } else if (e.getSource() == yesOverwrite) {
            jrar
                    .addToDebugWindow("Overwrite \'Yes\' button pressed, overwriteAnswer set to "
                            + JrarConstants.OVERWRITEYESANSWER);
            overwriteAnswer = JrarConstants.OVERWRITEYESANSWER;
            overwriteDialog.dispose();
        } else if (e.getSource() == noOverwrite) {
            jrar
                    .addToDebugWindow("Overwrite \'No\' button pressed, overwriteAnswer set to "
                            + JrarConstants.OVERWRITENOANSWER);
            overwriteAnswer = JrarConstants.OVERWRITENOANSWER;
            overwriteDialog.dispose();
        } else if (e.getSource() == allOverwrite) {
            jrar
                    .addToDebugWindow("Overwrite \'All\' button pressed, overwriteAnswer set to "
                            + JrarConstants.OVERWRITEALLANSWER);
            overwriteAnswer = JrarConstants.OVERWRITEALLANSWER;
            overwriteDialog.dispose();
        } else if (e.getSource() == neverOverwrite) {
            jrar
                    .addToDebugWindow("Overwrite \'Never\' button pressed, overwriteAnswer set to "
                            + JrarConstants.OVERWRITENEVERANSWER);
            overwriteAnswer = JrarConstants.OVERWRITENEVERANSWER;
            overwriteDialog.dispose();
        } else if (e.getSource() == quitOverwrite) {
            jrar
                    .addToDebugWindow("Overwrite \'Quit\' button pressed, overwriteAnswer set to "
                            + JrarConstants.OVERWRITEQUITANSWER);
            overwriteAnswer = JrarConstants.OVERWRITEQUITANSWER;
            overwriteDialog.dispose();
        }
    }

    private void openFileChooser() {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new RarFileFilter());
        int answer = fc.showOpenDialog(this);
        if (answer == JFileChooser.APPROVE_OPTION) {
            oldFile = currentFile;
            ftm.setNewData(emptyData);
            ftm.fireTableDataChanged();
            try {
                currentFile = fc.getSelectedFile().getCanonicalPath();
                getFileList();
            } catch (Exception exp) {
                updateStatusBar("Error 3: " + exp);
            }
        }
    }

    private void getFileList() {
        String op = "";
        try {
            Process p = Runtime.getRuntime().exec(
                    "rar vt  \"" + currentFile + "\"");
            InputStreamReader berr = new InputStreamReader(p.getInputStream());
            int temp = berr.read();
            do {
                op += (char) temp;
                temp = berr.read();
            } while (temp != -1);
        } catch (Exception ex) {
            updateStatusBar("Error 1: " + ex);
        }
        //  split the output into seperate strings after each new line
        String[] stringSplit = op.split("\\n");
        // trim all the whitespace off the lines
        for (int i = 0; i < stringSplit.length; i++)
            stringSplit[i] = stringSplit[i].trim();
        if (stringSplit[stringSplit.length - 1]
                .endsWith(".rar is not RAR archive")) {
            if (oldFile != null) {
                String of2 = currentFile;
                currentFile = oldFile;
                getFileList();
                updateStatusBar(of2 + " is not a RAR archive or is damaged");
            } else {
                updateStatusBar(currentFile
                        + " is not a RAR archive or is damaged");
            }
        } else {
            // create this to remove the leading junk we don't want
            String[] temp = new String[stringSplit.length - 11];
            // copy everything over except the first 10 lines and the last line
            for (int i = 0; i < stringSplit.length - 11; i++)
                temp[i] = stringSplit[i + 10];
            // set stringSplit to the updated version
            stringSplit = temp;
            // the real work starts here for every file there are 3 lines we
            // need to
            // get the data from. The last 2 lines of the stringSplit array are
            // info
            // on the whole archive and we don't need to worry about those yet
            // so we
            // take those off the length and then divide the total by 3
            // resulting in
            // the number of files in the archive.
            for (int i = 0; i < (stringSplit.length - 2) / 3; i++) {
                // the first thing we do it get the file name, this is the
                // first line
                // of the 3 we're looking at with indices 0, 3, 6, 9... and so
                // on
                String nextFileName = stringSplit[i * 3].trim();
                // System.out.println(nextFileName);
                // the next line in the list is the contatins a lot of info we
                // need as
                // well as a lot of whitespace. we pass this into
                // formatFileList which
                // removes the whitespace and returns a nice array for us
                String[] info = formatFileList(stringSplit[(i * 3) + 1]
                        .split(" "), "", 9);
                // the third line has some techinal info. This isn't used at
                // the moment
                // but we store it now for use later on
                String[] extraInfo = formatFileList(stringSplit[(i * 3) + 2]
                        .split(" "), "", 3);
                // combine the three things above to make it easier to work
                // with in the
                // table model
                String[] newRow = combineStringArrays(nextFileName, info,
                        extraInfo);
                // add the new row to the data in myTableModel
                ftm.addNewRow(newRow);
                // and fire a data change
                ftm.fireTableDataChanged();
            }
            // next we want to get the archive into and update the status bar
            String[] archiveInfo = formatFileList(
                    stringSplit[stringSplit.length - 1].split(" "), "", 4);
            updateStatusBar("Number of Files: " + archiveInfo[0]
                    + " Total Size: " + archiveInfo[1] + " Compressed Size: "
                    + archiveInfo[2] + " Ratio: " + archiveInfo[3]);
        }
    }

    private String[] combineStringArrays(String s1, String[] s2, String[] s3) {
        String[] s4 = new String[1 + s2.length + s3.length];
        s4[0] = s1;
        for (int i = 0; i < s2.length; i++) {
            s4[i + 1] = s2[i];
        }
        for (int i = 0; i < s3.length; i++) {
            s4[i + s2.length] = s3[i];
        }
        return s4;
    }

    private String[] formatFileList(String[] a, String s, int x) {
        String[] temp = new String[x];
        int j = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i].equals(s)) {
            } else {
                temp[j] = a[i].trim();
                j++;
            }
        }
        a = temp;
        return a;
    }

    public void updateStatusBar(String s) {
        jl.setText(s);
    }

    public void itemStateChanged(ItemEvent e) {
//      TODO
    }

    private void addFilesToArchive() {
        setButtonsState(false);
        afp = new AddingFilePanel(tablePanel, jrar, currentFile);
        tablePanel.add(afp, "Adding Card");
        CardLayout cl = (CardLayout) (tablePanel.getLayout());
        cl.last(tablePanel);
    }

    public void setButtonsState(boolean b) {
        openArchiveButton.setEnabled(b);
        addToArchiveButton.setEnabled(b);
        extractToButton.setEnabled(b);
        //testArchiveButton.setEnabled(b);
        //viewArchiveButton.setEnabled(b);
        //deleteFileButton.setEnabled(b);
        //protectButton.setEnabled(b);
        //sfxButton.setEnabled(b);
    }

    private void createNewArchivePopup() {
//      TODO
    }

    public void debuggingStopped() {
        buttonPanel.remove(buttonPanel.getComponent(buttonPanel
                .getComponentCount() - 1));
        buttonPanel.repaint();
        f.pack();
        f.setLocation((int) ((Toolkit.getDefaultToolkit().getScreenSize()
                .getWidth()) - f.getSize().getWidth()) / 2, (int) ((Toolkit
                .getDefaultToolkit().getScreenSize().getHeight()) - f.getSize()
                .getHeight()) / 2);
    }

    private void extractFilesTo() {
        if (currentFile != null) {
            JFileChooser fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int answer = fc.showDialog(this, "Extract here");
            if (answer == JFileChooser.APPROVE_OPTION) {
                try {
                    currentDir = "\"" + fc.getSelectedFile().getCanonicalPath() + "\"";
                } catch (Exception exp) {
                    updateStatusBar("Error 5: " + exp);
                }
                String selectedFiles = "";
                if (infoTable.getSelectedRowCount() != 0) {
                    jrar.addToDebugWindow("" + infoTable.getSelectedRowCount());
                    int[] filesToExtract = infoTable.getSelectedRows();
                    for (int i = 0; i < (filesToExtract.length); i++) {
                        selectedFiles += "\"" + infoTable.getValueAt(
                                filesToExtract[i], 0).toString()
                                + "\" ";
                        jrar.addToDebugWindow(selectedFiles);
                    }
                    selectedFiles.trim();
                } else if (infoTable.getSelectedRowCount() == 0) {
                    for (int i = 0; i < infoTable.getRowCount(); i++) {
                        selectedFiles += "\"" + infoTable.getValueAt(i, 0).toString()
                                +  "\" ";
                    }
                }
                jrar.newExtractionRarProcess(currentFile, selectedFiles,
                        currentDir);
            }
        } else {
            updateStatusBar("No open RAR file");
        }
    }

    public void extractionRarProcessFinishedSucessfully() {
        updateStatusBar("Extraction complete");
        setButtonsState(true);
        statusBar.remove(cancelOperation);
        statusBar.repaint();
    }
    
    public void addingRarProcessFinishedSucessfully() {
        updateStatusBar("Addition complete");
        setButtonsState(true);
        statusBar.remove(cancelOperation);
        statusBar.repaint();
        CardLayout cl = (CardLayout) (tablePanel.getLayout());
        tablePanel.remove(afp);
        cl.removeLayoutComponent(afp);
        cl.last(tablePanel);
        ftm.setNewData(emptyData);
        getFileList();
    }
    
    public void addCancelButtonToStatusBar(final RarProcess rp) {
        cancelOperation = new JButton("Cancel");
        statusBar.add(cancelOperation);
        cancelOperation.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              
            jrar.addToDebugWindow("Cancel button pressed");
            jrar.addToDebugWindow("Stopping process");
            rp.setStop(true);
            jrar.addToDebugWindow("Removing Cancel button from status bar");
            statusBar.remove(cancelOperation);
            jrar.addToDebugWindow("Validating status bar to update screen");
            setButtonsState(true);
            statusBar.revalidate();
          }
        });
        statusBar.validate();
        statusBar.repaint();
      }

    public String newRarFilePopup() {
        JFileChooser fc = new JFileChooser();
        fc.setMultiSelectionEnabled(false);
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fc.setFileFilter(new RarFileFilter());
        int answer = fc.showDialog(this, "Create new RAR file or add to existing one");

        if (answer == JFileChooser.APPROVE_OPTION) {
          String saveFile = fc.getSelectedFile().toString();
          if (saveFile.endsWith(".rar"))
              currentFile = saveFile;
          else
              currentFile = saveFile + ".rar";
          System.out.println("saveFile = " + saveFile);
          System.out.println("currentFile = " + currentFile);
        }
        
        else if (answer == JFileChooser.CANCEL_OPTION) {
            currentFile = "???CANCEL_ADDING_FILES???";
        }
        jrar.addToDebugWindow("Adding files to " + currentFile);
        return currentFile;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -