阅读Java中的所有文件夹并继续操作

时间:2017-01-28 07:14:24

标签: java encryption

这个Java解密(AES128)有效, 但我有两个问题:

  1. 任何时候文件损坏,其余文件都不会被解密。 (如果有10个文件且文件号4已损坏,则文件5-10不会被解密。)
  2. 如何阅读文件夹中的所有文件夹?
  3. public class DecoderGui extends javax.swing.JPanel {
    
    /**
     * Creates new form DecoderGui
     */
    
    public static void main(String[] args)
    {
        Security.addProvider(new BouncyCastleProvider());
        JFrame frame = new JFrame();
        frame.add( new DecoderGui());
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.setResizable( false );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }     
    
    public DecoderGui() {
        initComponents();
    }
    
    
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
    
        keyFile = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        sourceDirectory = new javax.swing.JTextField();
        destinationDirectory = new javax.swing.JTextField();
        keyButton = new javax.swing.JButton();
        sourceButton = new javax.swing.JButton();
        destinationButton = new javax.swing.JButton();
        decryptButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        message = new javax.swing.JLabel();
    
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Schutzstaffel Decryption");
    
        destinationDirectory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                destinationDirectoryActionPerformed(evt);
            }
        });
    
        keyButton.setText("Key File");
        keyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keyButtonActionPerformed(evt);
            }
        });
    
        sourceButton.setText("Browse");
        sourceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sourceButtonActionPerformed(evt);
            }
        });
    
        destinationButton.setText("Save");
        destinationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                destinationButtonActionPerformed(evt);
            }
        });
    
        decryptButton.setText("Decrypt");
        decryptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                decryptButtonActionPerformed(evt);
            }
        });
    
        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
    
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(29, 29, 29)
                                    .addComponent(decryptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(84, 84, 84)
                                    .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(keyFile, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                                    .addComponent(sourceDirectory)
                                    .addComponent(destinationDirectory))
                                .addGap(43, 43, 43)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(keyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(destinationButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(sourceButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(145, 145, 145)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(99, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(keyFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(keyButton))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sourceDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sourceButton))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(destinationDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(destinationButton))
                .addGap(27, 27, 27)
                .addComponent(message)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(decryptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(82, Short.MAX_VALUE))
        );
    }// </editor-fold>                        
    
    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          
    
    private void decryptButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        try {
            if(keyFile.getText() == null || keyFile.getText().equals("")) {
                message.setText("Please select the key file");
            }
            if(sourceDirectory.getText() == null || sourceDirectory.getText().equals("")) {
                message.setText("Please select the source folder");
            }
            if(destinationDirectory.getText() == null || destinationDirectory.getText().equals("")) {
                message.setText("Please select the destination folder");
            }
    
            Decoder decoder = new Decoder();
    
            File sourcedir = new File(sourceDirectory.getText());
            File destinationdir = new File(destinationDirectory.getText());
    
        //    System.out.println(seeder);
    
            String seeder = new String(decoder.getFileDataToStream(keyFile.getText()));
    
            System.out.println(seeder);
            File[] directoryListing = sourcedir.listFiles();
            if (directoryListing != null) {
                for (File child : directoryListing) {
                    System.out.println(child.getCanonicalPath());
                    byte[] inputstream = decoder.getFileDataToStream(child.getCanonicalPath());
                    byte[] outputstream = decoder.decrypt(seeder, inputstream);
                    FileOutputStream fos = new FileOutputStream(destinationDirectory.getText()+File.separator+new String(Base64.decodeBase64(child.getName())));
                    fos.write(outputstream);
                    fos.close();
    
                }
                message.setText("Decryption done");
            } else {
                message.setText("Error while Listing the Source Directory Files");
            }
        } catch (IOException ex) {
            Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }                                             
    
    private void keyButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Choose Key File");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
    
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            try {
                System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
                System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
                keyFile.setText(chooser.getSelectedFile().getCanonicalPath());
            } catch (IOException ex) {
                Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex);
            }
    
        } else {
          System.out.println("No Selection ");
        }
    }                                         
    
    private void sourceButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Choose Source Folder");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
    
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            try {
                System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
                System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
                sourceDirectory.setText(chooser.getSelectedFile().getCanonicalPath());
            } catch (IOException ex) {
                Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex);
            }
    
        } else {
          System.out.println("No Selection ");
        }
    }                                            
    
    private void destinationButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Choose Destination Folder");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
    
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            try {
                System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
                System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
                destinationDirectory.setText(chooser.getSelectedFile().getCanonicalPath());
            } catch (IOException ex) {
                Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex);
            }
    
        } else {
          System.out.println("No Selection ");
        }
    }                                                 
    
    private void destinationDirectoryActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        // TODO add your handling code here:
    } 
    

1 个答案:

答案 0 :(得分:0)

问题1:     如果解密文件时出错,则代码退出for循环,因此其他文件不会被解密,请参阅下面的更正代码,并添加代码代替ToDo:

private void decryptButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        try {
            if(keyFile.getText() == null || keyFile.getText().equals("")) {
                message.setText("Please select the key file");
            }
            if(sourceDirectory.getText() == null || sourceDirectory.getText().equals("")) {
                message.setText("Please select the source folder");
            }
            if(destinationDirectory.getText() == null || destinationDirectory.getText().equals("")) {
                message.setText("Please select the destination folder");
            }

            Decoder decoder = new Decoder();

            File sourcedir = new File(sourceDirectory.getText());
            File destinationdir = new File(destinationDirectory.getText());

        //    System.out.println(seeder);

            String seeder = new String(decoder.getFileDataToStream(keyFile.getText()));

            System.out.println(seeder);
            File[] directoryListing = sourcedir.listFiles();
            if (directoryListing != null) {
                for (File child : directoryListing) {
                    try{
                        System.out.println(child.getCanonicalPath());
                        byte[] inputstream = decoder.getFileDataToStream(child.getCanonicalPath());
                        byte[] outputstream = decoder.decrypt(seeder, inputstream);
                        FileOutputStream fos = new FileOutputStream(destinationDirectory.getText()+File.separator+new String(Base64.decodeBase64(child.getName())));
                        fos.write(outputstream);
                        fos.close();
                    }
                    catch(Exception e){
                        // ToDo: handle the case when file was not decrypted
                    }
                }
                message.setText("Decryption done");
            } else {
                message.setText("Error while Listing the Source Directory Files");
            }
        } catch (IOException ex) {
            Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DecoderGui.class.getName()).log(Level.SEVERE, null, ex);
        }

    }   

问题2:获取文件夹中的所有文件夹。完整的文件夹层次结构

LinkedList<String> folders = new LinkedList<String>();

folders.add("c:\\");

while(!folders.isEmpty()){

    File file = new File(folders.poll());

    if(file.listFiles()!=null)
        for(File dir:file.listFiles()){

            if(dir.isDirectory()){
                folders.add(dir.getAbsolutePath());
                System.out.println("Folder " + dir.getAbsolutePath());
            }
        }

}