From 2ff0ec353ebac9ddc518b18daa09c431b0c49e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Clermont=C3=A9?= Date: Tue, 12 Apr 2016 00:41:43 +0200 Subject: [PATCH] added checkDialog --- src/org/dclermonte/siba/gui/CheckDialog.java | 76 ++++++ src/org/dclermonte/siba/gui/CheckPanel.java | 46 +++- .../dclermonte/siba/model/SibaManager.java | 224 ++++++++++-------- 3 files changed, 236 insertions(+), 110 deletions(-) create mode 100644 src/org/dclermonte/siba/gui/CheckDialog.java diff --git a/src/org/dclermonte/siba/gui/CheckDialog.java b/src/org/dclermonte/siba/gui/CheckDialog.java new file mode 100644 index 0000000..4f73bf9 --- /dev/null +++ b/src/org/dclermonte/siba/gui/CheckDialog.java @@ -0,0 +1,76 @@ +package org.dclermonte.siba.gui; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; + +public class CheckDialog extends JDialog +{ + + public static Object lblNewLabel; + public static String message; + + /** + * Launch the application. + */ + public static void main(final String string) + { + message = string; + try + { + CheckDialog dialog = new CheckDialog(); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + private final JPanel contentPanel = new JPanel(); + + /** + * Create the dialog. + */ + public CheckDialog() + { + setBounds(100, 100, 450, 300); + getContentPane().setLayout(new BorderLayout()); + this.contentPanel.setLayout(new FlowLayout()); + this.contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(this.contentPanel, BorderLayout.CENTER); + { + JLabel lblNewLabel = new JLabel(message); + this.contentPanel.add(lblNewLabel); + } + { + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + { + JButton okButton = new JButton("OK"); + okButton.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(final ActionEvent e) + { + setVisible(false); + dispose(); + } + }); + okButton.setActionCommand("OK"); + buttonPane.add(okButton); + getRootPane().setDefaultButton(okButton); + } + } + } + +} diff --git a/src/org/dclermonte/siba/gui/CheckPanel.java b/src/org/dclermonte/siba/gui/CheckPanel.java index f83879b..8d1358d 100644 --- a/src/org/dclermonte/siba/gui/CheckPanel.java +++ b/src/org/dclermonte/siba/gui/CheckPanel.java @@ -32,6 +32,7 @@ public class CheckPanel extends JPanel { private static final long serialVersionUID = -1580485684838045920L; private JTextField textField; + private File choosenFile; /** * Create the panel. @@ -111,41 +112,60 @@ public class CheckPanel extends JPanel final void check() throws IOException, NoSuchAlgorithmException { - File file = new File(this.textField.getText()); - FileReader fileReader = new FileReader(file); + FileReader fileReader = new FileReader(this.choosenFile); char[] md5 = new char[32]; byte[] md5byte = new byte[32]; - char[] fileToCheck = new char[(int) (file.length()) - 32]; + char[] fileToCheck = new char[(int) (this.choosenFile.length()) - 32]; + String md5String = new String(); for (int index = 0; index < 32; index++) { md5[index] = (char) fileReader.read(); md5byte[index] = (byte) md5[index]; + md5String = md5String + md5[index]; } fileReader.read(); String fileNameToString = new String(); - for (int index = 36; index < (file.length() + 3); index++) + for (int index = 36; index < (this.choosenFile.length() + 3); index++) { fileToCheck[index - 36] = (char) fileReader.read(); + fileNameToString = fileNameToString + fileToCheck[index - 36]; } - fileNameToString = String.copyValueOf(fileToCheck); + fileReader.close(); - byte[] bytedirectoryToSave = new byte[fileToCheck.length]; - FileInputStream fileInputStream = new FileInputStream(file); - for (int index1 = 0; index1 < fileNameToString.length(); index1++) + File fileToCheck1 = new File(this.choosenFile.getParent() + "/" + fileNameToString); + byte[] bytedirectoryToSave = new byte[(int) fileToCheck1.length()]; + FileInputStream fileInputStream = new FileInputStream(fileToCheck1); + for (int index1 = 0; index1 < fileToCheck1.length(); index1++) { bytedirectoryToSave[index1] = (byte) fileInputStream.read(); } fileInputStream.close(); byte[] hash = null; + StringBuilder hashString = new StringBuilder(); MessageDigest messageDigest = MessageDigest.getInstance("MD5"); hash = messageDigest.digest(bytedirectoryToSave); - if (MessageDigest.isEqual(hash, md5byte)) + for (int index = 0; index < hash.length; index++) { - System.out.println("gagné"); + String hex = Integer.toHexString(hash[index]); + if (hex.length() == 1) + { + hashString.append('0'); + hashString.append(hex.charAt(hex.length() - 1)); + } + else + { + hashString.append(hex.substring(hex.length() - 2)); + } + } + + if (md5String.equals(hashString.toString())) + { + CheckDialog.main("Vérification OK"); + } else { - System.out.println("Perdu"); + CheckDialog.main("Il y a une erreur"); } } @@ -166,8 +186,8 @@ public class CheckPanel extends JPanel if (returnVal == JFileChooser.APPROVE_OPTION) { - File file = fc.getSelectedFile(); - this.textField.setText(file.getName()); + this.choosenFile = fc.getSelectedFile(); + this.textField.setText(this.choosenFile.getName()); } } diff --git a/src/org/dclermonte/siba/model/SibaManager.java b/src/org/dclermonte/siba/model/SibaManager.java index 3d12537..0f4736f 100644 --- a/src/org/dclermonte/siba/model/SibaManager.java +++ b/src/org/dclermonte/siba/model/SibaManager.java @@ -20,116 +20,146 @@ import org.apache.commons.compress.archivers.ArchiveOutputStream; 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.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.utils.IOUtils; - public class SibaManager { -public static File backup(File toto,String destination) throws ArchiveException, IOException{ -/* 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); - os.close(); - md5(output,destination+"/"+outputFileNameWithoutExtension+".md5"); - return output; -} -public static void directoryToSave(File directory,ArchiveOutputStream outputStream) throws IOException{ - for(File file:directory.listFiles()){ - if (file.isDirectory()){ - directoryToSave(file,outputStream); - }else{ - outputStream.putArchiveEntry(new TarArchiveEntry(file)); - IOUtils.copy(new FileInputStream(file), outputStream); - outputStream.closeArchiveEntry(); - } - } - -} -public static File md5(File directoryToSave,String destination) throws IOException{ - File saved = new File(destination); - try + public static File backup(final File toto, final String target) throws ArchiveException, IOException { - 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); - StringBuilder hashString = new StringBuilder(); - for (int index = 0; index < hash.length; index++) - { - String hex = Integer.toHexString(hash[index]); - if (hex.length() == 1) - { - hashString.append('0'); - hashString.append(hex.charAt(hex.length() - 1)); - } - else - hashString.append(hex.substring(hex.length() - 2)); - } - FileWriter fileWriter = new FileWriter(saved); - fileWriter.write(hashString.toString()); - fileWriter.append(" " + directoryToSave.getName()); - fileWriter.close(); - } - catch (NoSuchAlgorithmException e) - { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return saved; -} -public static void extract(File directoryToUntar, File destination) throws IOException, ArchiveException{ - File file = destination; + /* 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 + ".tar"; + File output = new File(target + "/" + outputFileName); + File output1 = new File(target + "/" + outputFileNameWithoutExtension + ".tgz"); - final List untaredFiles = new LinkedList(); - InputStream inputStream; + final OutputStream out = new FileOutputStream(target + "/" + outputFileName); + ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out); + directoryToSave(toto, os); + os.close(); + final OutputStream out1 = new FileOutputStream(target + "/" + outputFileNameWithoutExtension + ".tgz"); + final GzipCompressorOutputStream gzCompress = new GzipCompressorOutputStream(out1); + byte[] fileTar = new byte[(int) output.length()]; + FileInputStream fileReader = new FileInputStream(output); + fileReader.read(fileTar); + /* byte[] fileTarByte = new byte[fileTar.length]; + for (int i = 0; i < fileTarByte.length; i++) + { + fileTarByte[i] = (byte) fileTar[i]; + } */ + gzCompress.write(fileTar); + gzCompress.close(); + md5(output1, target + "/" + outputFileNameWithoutExtension + ".md5"); + return output1; + } + + public static void directoryToSave(final File directory, final ArchiveOutputStream outputStream) throws IOException + { + for (File file : directory.listFiles()) + { + if (file.isDirectory()) + { + directoryToSave(file, outputStream); + } + else + { + outputStream.putArchiveEntry(new TarArchiveEntry(file)); + IOUtils.copy(new FileInputStream(file), outputStream); + outputStream.closeArchiveEntry(); + } + } + + } + + public static void extract(final File directoryToUntar, final File destination) throws IOException, ArchiveException + { + File file = destination; + + final List untaredFiles = new LinkedList(); + InputStream inputStream; try { inputStream = new FileInputStream(directoryToUntar); - - - final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream); - TarArchiveEntry entry = null; - while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) { - final File outputFile = new File(file, entry.getName()); - if (entry.isDirectory()) { - if (!outputFile.exists()) { - if (!outputFile.mkdirs()) { - throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); - } - } - } else { - final OutputStream outputFileStream = new FileOutputStream(outputFile); - IOUtils.copy(debInputStream, outputFileStream); - outputFileStream.close(); - } - untaredFiles.add(outputFile); - } - debInputStream.close(); } - catch (FileNotFoundException e) + + final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() + .createArchiveInputStream("tar", inputStream); + TarArchiveEntry entry = null; + while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) + { + final File outputFile = new File(file, entry.getName()); + if (entry.isDirectory()) + { + if (!outputFile.exists()) + { + if (!outputFile.mkdirs()) + { + throw new IllegalStateException( + String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); + } + } + } + else + { + final OutputStream outputFileStream = new FileOutputStream(outputFile); + IOUtils.copy(debInputStream, outputFileStream); + outputFileStream.close(); + } + untaredFiles.add(outputFile); + } + debInputStream.close(); + } + catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } - } + } + + public static File md5(final File directoryToSave, final String destination) throws IOException + { + File saved = new File(destination); + try + { + 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); + StringBuilder hashString = new StringBuilder(); + for (int index = 0; index < hash.length; index++) + { + String hex = Integer.toHexString(hash[index]); + if (hex.length() == 1) + { + hashString.append('0'); + hashString.append(hex.charAt(hex.length() - 1)); + } + else + { + hashString.append(hex.substring(hex.length() - 2)); + } + } + FileWriter fileWriter = new FileWriter(saved); + fileWriter.write(hashString.toString()); + fileWriter.append(" " + directoryToSave.getName()); + fileWriter.close(); + } + catch (NoSuchAlgorithmException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return saved; + } } - -