added checkDialog
This commit is contained in:
parent
3a1d366474
commit
2ff0ec353e
76
src/org/dclermonte/siba/gui/CheckDialog.java
Normal file
76
src/org/dclermonte/siba/gui/CheckDialog.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -32,6 +32,7 @@ public class CheckPanel extends JPanel
|
|||||||
{
|
{
|
||||||
private static final long serialVersionUID = -1580485684838045920L;
|
private static final long serialVersionUID = -1580485684838045920L;
|
||||||
private JTextField textField;
|
private JTextField textField;
|
||||||
|
private File choosenFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the panel.
|
* Create the panel.
|
||||||
@ -111,41 +112,60 @@ public class CheckPanel extends JPanel
|
|||||||
|
|
||||||
final void check() throws IOException, NoSuchAlgorithmException
|
final void check() throws IOException, NoSuchAlgorithmException
|
||||||
{
|
{
|
||||||
File file = new File(this.textField.getText());
|
FileReader fileReader = new FileReader(this.choosenFile);
|
||||||
FileReader fileReader = new FileReader(file);
|
|
||||||
char[] md5 = new char[32];
|
char[] md5 = new char[32];
|
||||||
byte[] md5byte = new byte[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++)
|
for (int index = 0; index < 32; index++)
|
||||||
{
|
{
|
||||||
md5[index] = (char) fileReader.read();
|
md5[index] = (char) fileReader.read();
|
||||||
md5byte[index] = (byte) md5[index];
|
md5byte[index] = (byte) md5[index];
|
||||||
|
md5String = md5String + md5[index];
|
||||||
}
|
}
|
||||||
fileReader.read();
|
fileReader.read();
|
||||||
String fileNameToString = new String();
|
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();
|
fileToCheck[index - 36] = (char) fileReader.read();
|
||||||
|
fileNameToString = fileNameToString + fileToCheck[index - 36];
|
||||||
}
|
}
|
||||||
fileNameToString = String.copyValueOf(fileToCheck);
|
|
||||||
fileReader.close();
|
fileReader.close();
|
||||||
byte[] bytedirectoryToSave = new byte[fileToCheck.length];
|
File fileToCheck1 = new File(this.choosenFile.getParent() + "/" + fileNameToString);
|
||||||
FileInputStream fileInputStream = new FileInputStream(file);
|
byte[] bytedirectoryToSave = new byte[(int) fileToCheck1.length()];
|
||||||
for (int index1 = 0; index1 < fileNameToString.length(); index1++)
|
FileInputStream fileInputStream = new FileInputStream(fileToCheck1);
|
||||||
|
for (int index1 = 0; index1 < fileToCheck1.length(); index1++)
|
||||||
{
|
{
|
||||||
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
|
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
|
||||||
}
|
}
|
||||||
fileInputStream.close();
|
fileInputStream.close();
|
||||||
byte[] hash = null;
|
byte[] hash = null;
|
||||||
|
StringBuilder hashString = new StringBuilder();
|
||||||
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
||||||
hash = messageDigest.digest(bytedirectoryToSave);
|
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
|
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)
|
if (returnVal == JFileChooser.APPROVE_OPTION)
|
||||||
{
|
{
|
||||||
File file = fc.getSelectedFile();
|
this.choosenFile = fc.getSelectedFile();
|
||||||
this.textField.setText(file.getName());
|
this.textField.setText(this.choosenFile.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,116 +20,146 @@ import org.apache.commons.compress.archivers.ArchiveOutputStream;
|
|||||||
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
|
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
|
||||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||||
|
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
|
||||||
import org.apache.commons.compress.utils.IOUtils;
|
import org.apache.commons.compress.utils.IOUtils;
|
||||||
|
|
||||||
|
|
||||||
public class SibaManager
|
public class SibaManager
|
||||||
{
|
{
|
||||||
|
|
||||||
public static File backup(File toto,String destination) throws ArchiveException, IOException{
|
public static File backup(final File toto, final String target) 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
|
|
||||||
{
|
{
|
||||||
byte[] bytedirectoryToSave = new byte[(int) directoryToSave.length()] ;
|
/* String outputFileNameWithoutExtension = toto.getName()+"-"+ LocalDateTime.now(); */
|
||||||
FileInputStream fileInputStream = new FileInputStream(directoryToSave);
|
LocalDateTime date = LocalDateTime.now();
|
||||||
for ( int index1 = 0 ;index1< directoryToSave.length();index1++)
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'_'H'H_'m");
|
||||||
{
|
String text = date.format(formatter);
|
||||||
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
|
String outputFileNameWithoutExtension = toto.getName() + "-" + text;
|
||||||
}
|
String outputFileName = outputFileNameWithoutExtension + ".tar";
|
||||||
|
File output = new File(target + "/" + outputFileName);
|
||||||
|
File output1 = new File(target + "/" + outputFileNameWithoutExtension + ".tgz");
|
||||||
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;
|
|
||||||
|
|
||||||
final List<File> untaredFiles = new LinkedList<File>();
|
final OutputStream out = new FileOutputStream(target + "/" + outputFileName);
|
||||||
InputStream inputStream;
|
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<File> untaredFiles = new LinkedList<File>();
|
||||||
|
InputStream inputStream;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
inputStream = new FileInputStream(directoryToUntar);
|
inputStream = new FileInputStream(directoryToUntar);
|
||||||
|
|
||||||
|
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
|
||||||
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream);
|
.createArchiveInputStream("tar", inputStream);
|
||||||
TarArchiveEntry entry = null;
|
TarArchiveEntry entry = null;
|
||||||
while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
|
while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null)
|
||||||
final File outputFile = new File(file, entry.getName());
|
{
|
||||||
if (entry.isDirectory()) {
|
final File outputFile = new File(file, entry.getName());
|
||||||
if (!outputFile.exists()) {
|
if (entry.isDirectory())
|
||||||
if (!outputFile.mkdirs()) {
|
{
|
||||||
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
|
if (!outputFile.exists())
|
||||||
}
|
{
|
||||||
}
|
if (!outputFile.mkdirs())
|
||||||
} else {
|
{
|
||||||
final OutputStream outputFileStream = new FileOutputStream(outputFile);
|
throw new IllegalStateException(
|
||||||
IOUtils.copy(debInputStream, outputFileStream);
|
String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
|
||||||
outputFileStream.close();
|
}
|
||||||
}
|
}
|
||||||
untaredFiles.add(outputFile);
|
}
|
||||||
}
|
else
|
||||||
debInputStream.close(); }
|
{
|
||||||
catch (FileNotFoundException e)
|
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
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user