Updated md5 + checkpanel

This commit is contained in:
Didier Clermonté 2016-04-07 00:54:03 +02:00
parent 08acb51455
commit 3472d2d032
5 changed files with 201 additions and 47 deletions

View File

@ -22,11 +22,22 @@ import com.jgoodies.forms.layout.Sizes;
import org.apache.commons.compress.archivers.ArchiveException;
import org.dclermonte.siba.model.*;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import java.awt.FlowLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.util.ResourceBundle;
public class BackupPanel extends JPanel
{
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.gui.messages"); //$NON-NLS-1$
private JTextField textField;
public JTextField textField_1;
private JRadioButton rdbtnNewRadioButton_1;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Create the panel.
@ -34,8 +45,47 @@ public class BackupPanel extends JPanel
public BackupPanel()
{
setLayout(new BorderLayout(0, 0));
JButton btnNewButton = new JButton("...");
JPanel panel_4 = new JPanel();
add(panel_4, BorderLayout.NORTH);
JLabel lblNewLabel_2 = new JLabel(BUNDLE.getString("BackupPanel.lblNewLabel_2.text")); //$NON-NLS-1$
panel_4.add(lblNewLabel_2);
JLabel lblLeRepertoire = new JLabel(BUNDLE.getString("BackupPanel.lblLeRepertoire.text")); //$NON-NLS-1$
panel_4.add(lblLeRepertoire);
JPanel panel = new JPanel();
add(panel);
panel.setLayout(new FormLayout(new ColumnSpec[] {
FormSpecs.RELATED_GAP_COLSPEC,
FormSpecs.DEFAULT_COLSPEC,
FormSpecs.RELATED_GAP_COLSPEC,
ColumnSpec.decode("default:grow"),
FormSpecs.RELATED_GAP_COLSPEC,
FormSpecs.DEFAULT_COLSPEC,},
new RowSpec[] {
FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,
FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,
FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,
FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,}));
JLabel lblNewLabel = new JLabel(BUNDLE.getString("BackupPanel.lblNewLabel.text")); //$NON-NLS-1$
panel.add(lblNewLabel, "2, 2");
lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel.setVerticalAlignment(SwingConstants.TOP);
textField = new JTextField();
panel.add(textField, "4, 2");
textField.setColumns(10);
JButton btnNewButton = new JButton(BUNDLE.getString("BackupPanel.btnNewButton.text")); //$NON-NLS-1$
panel.add(btnNewButton, "6, 2");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
@ -48,36 +98,18 @@ public class BackupPanel extends JPanel
}
}
});
setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("center:2dlu"),
FormSpecs.DEFAULT_COLSPEC,
FormSpecs.RELATED_GAP_COLSPEC,
ColumnSpec.decode("default:grow"),
FormSpecs.RELATED_GAP_COLSPEC,
FormSpecs.DEFAULT_COLSPEC,},
new RowSpec[] {
RowSpec.decode("5dlu"),
RowSpec.decode("25px"),
FormSpecs.DEFAULT_ROWSPEC,
RowSpec.decode("25px"),
FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,
FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,
FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,}));
JLabel lblNewLabel = new JLabel("Source Directory:");
lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel.setVerticalAlignment(SwingConstants.TOP);
add(lblNewLabel, "2, 2, left, center");
JLabel lblRpertoireDeDestination = new JLabel(BUNDLE.getString("BackupPanel.lblRpertoireDeDestination.text")); //$NON-NLS-1$
panel.add(lblRpertoireDeDestination, "2, 4");
lblRpertoireDeDestination.setVerticalAlignment(SwingConstants.TOP);
lblRpertoireDeDestination.setHorizontalAlignment(SwingConstants.LEFT);
textField = new JTextField();
add(textField, "4, 2, fill, default");
textField.setColumns(10);
add(btnNewButton, "6, 2, left, top");
textField_1 = new JTextField();
panel.add(textField_1, "4, 4");
textField_1.setColumns(10);
JButton btnNewButton_1 = new JButton("...");
JButton btnNewButton_1 = new JButton(BUNDLE.getString("BackupPanel.btnNewButton_1.text")); //$NON-NLS-1$
panel.add(btnNewButton_1, "6, 4");
btnNewButton_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e1) {
@ -90,24 +122,52 @@ public class BackupPanel extends JPanel
}
}
});
JLabel lblRpertoireDeDestination = new JLabel("Target Directory:");
lblRpertoireDeDestination.setVerticalAlignment(SwingConstants.TOP);
lblRpertoireDeDestination.setHorizontalAlignment(SwingConstants.LEFT);
add(lblRpertoireDeDestination, "2, 4, left, default");
textField_1 = new JTextField();
add(textField_1, "4, 4, fill, default");
textField_1.setColumns(10);
btnNewButton_1.setHorizontalAlignment(SwingConstants.RIGHT);
add(btnNewButton_1, "6, 4, left, top");
JButton btnNewButton_2 = new JButton("Backup");
JLabel lblNewLabel_1 = new JLabel(BUNDLE.getString("BackupPanel.lblNewLabel_1.text")); //$NON-NLS-1$
panel.add(lblNewLabel_1, "2, 6, right, top");
JPanel panel_3 = new JPanel();
panel.add(panel_3, "4, 6, fill, fill");
panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));
JRadioButton rdbtnNewRadioButton = new JRadioButton(BUNDLE.getString("BackupPanel.rdbtnNewRadioButton.text")); //$NON-NLS-1$
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setSelected(true);
panel_3.add(rdbtnNewRadioButton);
rdbtnNewRadioButton_1 = new JRadioButton(BUNDLE.getString("BackupPanel.rdbtnNewRadioButton_1.text")); //$NON-NLS-1$
buttonGroup.add(rdbtnNewRadioButton_1);
panel_3.add(rdbtnNewRadioButton_1);
JPanel panel_2 = new JPanel();
add(panel_2, BorderLayout.SOUTH);
panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
Component verticalStrut_1 = Box.createVerticalStrut(5);
panel_2.add(verticalStrut_1);
JPanel panel_1 = new JPanel();
panel_2.add(panel_1);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
Component horizontalGlue = Box.createHorizontalGlue();
panel_1.add(horizontalGlue);
JButton btnNewButton_2 = new JButton(BUNDLE.getString("BackupPanel.btnNewButton_2.text")); //$NON-NLS-1$
panel_1.add(btnNewButton_2);
Component horizontalGlue_1 = Box.createHorizontalGlue();
panel_1.add(horizontalGlue_1);
Component verticalStrut = Box.createVerticalStrut(20);
panel_2.add(verticalStrut);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
SibaManager.backup(new File(textField.getText()),textField_1.getText());
}
catch (ArchiveException | IOException e1)
{
@ -116,7 +176,6 @@ public class BackupPanel extends JPanel
}
}
});
add(btnNewButton_2, "4, 10");
}

View File

@ -7,13 +7,23 @@ import javax.swing.JFileChooser;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
public class CheckPanel extends JPanel
{
@ -42,7 +52,20 @@ public class CheckPanel extends JPanel
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
repertoireChoisi();
try
{
repertoireChoisi();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (NoSuchAlgorithmException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
@ -56,15 +79,51 @@ public class CheckPanel extends JPanel
}
final void repertoireChoisi() {
final void repertoireChoisi() throws IOException, NoSuchAlgorithmException {
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichiers md5","md5");
fc.addChoosableFileFilter(filter);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = fc.showDialog(CheckPanel.this,"Sauvegarde à vérifier");
if (returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
textField.setText(file.getPath());}
textField.setText(file.getPath());
FileReader fileReader = new FileReader(file);
char[] md5 = new char[32];
byte[] md5byte = new byte[32];
char[] fileToCheck = new char[(int) (file.length())-32];
for (int index=0;index<32;index++){
md5[index]= (char) fileReader.read();
md5byte[index]=(byte) md5[index] ;
}
fileReader.read();
String fileNameToString = new String();
for (int index=36;index<file.length()+3;index++){
fileToCheck[index-36]= (char) fileReader.read();
}
fileNameToString= String.copyValueOf(fileToCheck);
File fileToCheck1 = new File(fileNameToString);
fileReader.close();
byte[] bytedirectoryToSave = new byte[(int) fileToCheck.length] ;
FileInputStream fileInputStream = new FileInputStream(file);
for ( int index1 = 0 ;index1< fileNameToString.length();index1++)
{
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
}
byte[] hash = null;
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
hash = messageDigest.digest(bytedirectoryToSave);
if (messageDigest.isEqual(hash, md5byte)){
System.out.println("gagné");}
else {
System.out.println("Perdu");
}
}
}
}

View File

@ -0,0 +1,12 @@
#Field ResourceBundle: BUNDLE
#Mon Apr 04 22:42:06 CEST 2016
BackupPanel.btnNewButton.text=\u2026
BackupPanel.btnNewButton_1.text=\u2026
BackupPanel.btnNewButton_2.text=Backup
BackupPanel.lblLeRepertoire.text=- le repertoire \u00E0 compresser
BackupPanel.lblNewLabel.text=Source Directory\:
BackupPanel.lblNewLabel_1.text=Date Format
BackupPanel.lblNewLabel_2.text=Choisissez \:
BackupPanel.lblRpertoireDeDestination.text=Target Directory\:
BackupPanel.rdbtnNewRadioButton.text=(iso8601) YYYY-MM-dd
BackupPanel.rdbtnNewRadioButton_1.text=Human

View File

@ -0,0 +1,10 @@
#Field ResourceBundle: BUNDLE
#Fri Apr 01 15:52:27 CEST 2016
BackupPanel.btnNewButton.text=\u2026
BackupPanel.btnNewButton_1.text=...
BackupPanel.btnNewButton_2.text=Backup
BackupPanel.lblNewLabel.text=Source Directory\:
BackupPanel.lblNewLabel_1.text=Date Format
BackupPanel.lblRpertoireDeDestination.text=Target Directory\:
BackupPanel.rdbtnNewRadioButton.text=(iso8601) YYYY-MM-dd
BackupPanel.rdbtnNewRadioButton_1.text=Human

View File

@ -11,6 +11,7 @@ import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;
@ -20,17 +21,21 @@ import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.dclermonte.siba.gui.BackupPanel;
public class SibaManager
{
public static File backup(File toto,String destination) throws ArchiveException, IOException{
String outputFileNameWithoutExtension = toto.getName()+"-"+ LocalDateTime.now();
/* String outputFileNameWithoutExtension = toto.getName()+"-"+ LocalDateTime.now(); */
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'_'H'H_'m");
String text = date.format(formatter);
String outputFileNameWithoutExtension = toto.getName()+"-"+ text;
String outputFileName = outputFileNameWithoutExtension + ".tgz";
File output = new File(destination+"/"+outputFileName);
final OutputStream out = new FileOutputStream(destination+"/"+outputFileName);
ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out);
directoryToSave(toto,os);
@ -53,7 +58,15 @@ public static void directoryToSave(File directory,ArchiveOutputStream outputStre
public static File md5(File directoryToSave,String destination) throws IOException{
File saved = new File(destination);
try
{ byte[] bytedirectoryToSave = directoryToSave.toString().getBytes() ;
{
byte[] bytedirectoryToSave = new byte[(int) directoryToSave.length()] ;
FileInputStream fileInputStream = new FileInputStream(directoryToSave);
for ( int index1 = 0 ;index1< directoryToSave.length();index1++)
{
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
}
byte[] hash = null;
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
hash = messageDigest.digest(bytedirectoryToSave);
@ -71,6 +84,7 @@ public static File md5(File directoryToSave,String destination) throws IOExcep
}
FileWriter fileWriter = new FileWriter(saved);
fileWriter.write(hashString.toString());
fileWriter.append(" " + directoryToSave.getName());
fileWriter.close();
}
catch (NoSuchAlgorithmException e)