Check move to manager

This commit is contained in:
Didier Clermonté 2016-05-02 22:28:35 +02:00
parent e52c320b59
commit cf30fed8bb
9 changed files with 180 additions and 154 deletions

BIN
lib/Siba.jar Normal file

Binary file not shown.

3
lib/Siba.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
java -jar Siba.jar $1 $2

View File

@ -19,6 +19,7 @@
*/ */
package org.dclermonte.siba; package org.dclermonte.siba;
import org.dclermonte.siba.cli.SibaCLI;
import org.dclermonte.siba.gui.SibaGUI; import org.dclermonte.siba.gui.SibaGUI;
public class Siba public class Siba
@ -26,13 +27,17 @@ public class Siba
public static void main(final String[] args) public static void main(final String[] args)
{ {
if (args.length == 0) if (args.length == 0)
{ {
SibaGUI.main(args); SibaGUI.main(args);
;
} }
else
{
SibaCLI.main(args);
}
} }
} }

View File

@ -19,7 +19,42 @@
*/ */
package org.dclermonte.siba.cli; package org.dclermonte.siba.cli;
import org.dclermonte.siba.gui.SibaGUI;
public class SibaCLI public class SibaCLI
{ {
public static void help()
{
System.out.println("usage : ");
System.out.println("Siba : ");
System.out.println("Siba [-h |-help| --help]");
System.out.println("siba -backup -directoryToSave [-target]");
System.out.println("siba -check [-filename | -file.tgz | -file.md5]");
}
public static void main(final String[] args)
{
if (args.length == 0)
{
SibaGUI.main(args);
}
for (String arg : args)
{
if (arg.equals("-h") || args.equals("-help") || args.equals("--help"))
{
help();
break;
}
if (arg.equals("-backup"))
{
}
}
}
} }

View File

@ -165,25 +165,35 @@ public class BackupPanel extends JPanel
panel_2.add(verticalStrut); panel_2.add(verticalStrut);
btnNewButton_2.addActionListener(new ActionListener() btnNewButton_2.addActionListener(new ActionListener()
{ {
private Object e1;
@Override @Override
public void actionPerformed(final ActionEvent e) public void actionPerformed(final ActionEvent e)
{ {
try try
{ {
File output1 = SibaManager.backup(new File(BackupPanel.this.textField.getText()), File directoryToSave1 = new File(BackupPanel.this.textField.getText());
BackupPanel.this.textField_1.getText()); File targetDirectory = new File(BackupPanel.this.textField_1.getText());
if (output1 == null) if (directoryToSave1.exists() && targetDirectory.exists())
{ {
return; File output1 = SibaManager.backup(directoryToSave1, BackupPanel.this.textField_1.getText());
String message = BUNDLE.getString("BackupPanel.confirmDialogMessage.text") + output1.getName();
String titre = BUNDLE.getString("BackupPanel.confirmDialogTitle.text");
JOptionPane.showMessageDialog(null, message, titre, JOptionPane.INFORMATION_MESSAGE);
}
else
{
String dataMissing = BUNDLE.getString("BackupPanel.dataMissing.text");
String titleWarning = BUNDLE.getString("BackupPanel.TitleWarning.text");
JOptionPane.showMessageDialog(null, dataMissing, titleWarning, JOptionPane.INFORMATION_MESSAGE);
} }
String message = BUNDLE.getString("BackupPanel.confirmDialogMessage.text") + output1.getName();
String titre = BUNDLE.getString("BackupPanel.confirmDialogTitle.text");
JOptionPane.showMessageDialog(null, message, titre, JOptionPane.INFORMATION_MESSAGE);
} }
catch (ArchiveException | IOException e1) catch (ArchiveException | IOException e1)
{ {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e1.printStackTrace(); ((Throwable) this.e1).printStackTrace();
} }
} }
}); });

View File

@ -23,10 +23,7 @@ import java.awt.Cursor;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -36,6 +33,8 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JTextField; import javax.swing.JTextField;
import org.dclermonte.siba.model.SibaManager;
import com.jgoodies.forms.layout.ColumnSpec; import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout; import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs; import com.jgoodies.forms.layout.FormSpecs;
@ -84,7 +83,8 @@ public class CheckPanel extends JPanel
{ {
try try
{ {
choosenDirectory(); CheckPanel.this.choosenFile = choosenDirectory();
} }
catch (IOException e1) catch (IOException e1)
{ {
@ -113,7 +113,35 @@ public class CheckPanel extends JPanel
{ {
try try
{ {
check();
if (!(CheckPanel.this.choosenFile == null) && CheckPanel.this.choosenFile.exists())
{
boolean check = SibaManager.check(CheckPanel.this.choosenFile);
String titre = BUNDLE.getString("CheckPanel.confirmDialogTitle.text");
String yourFile = BUNDLE.getString("CheckPanel.confirmDialogYourString.text");
if (check)
{
String message = yourFile + CheckPanel.this.choosenFile.getName() + " "
+ BUNDLE.getString("CheckPanel.confirmDialogGood.text");
JOptionPane.showMessageDialog(null, message, titre, JOptionPane.INFORMATION_MESSAGE);
}
else
{
String message = yourFile + CheckPanel.this.choosenFile.getName() + " "
+ BUNDLE.getString("CheckPanel.confirmDialogBad.text");
JOptionPane.showMessageDialog(null, message, titre, JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
String dataMissing = BUNDLE.getString("BackupPanel.dataMissing.text");
String titleWarning = BUNDLE.getString("BackupPanel.TitleWarning.text");
JOptionPane.showMessageDialog(null, dataMissing, titleWarning, JOptionPane.INFORMATION_MESSAGE);
}
} }
catch (NoSuchAlgorithmException e) catch (NoSuchAlgorithmException e)
{ {
@ -131,84 +159,21 @@ public class CheckPanel extends JPanel
} }
final void check() throws IOException, NoSuchAlgorithmException public File choosenDirectory() throws IOException, NoSuchAlgorithmException
{ {
FileReader fileReader = new FileReader(this.choosenFile); File result;
char[] md5 = new char[32];
byte[] md5byte = new byte[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 < (this.choosenFile.length() + 2); index++)
{
fileToCheck[index - 36] = (char) fileReader.read();
fileNameToString = fileNameToString + fileToCheck[index - 36];
}
fileReader.close(); this.choosenFile = CheckDirectorySelector.showSelectorDialog(null, null);
File fileToCheck1 = new File(this.choosenFile.getParent() + "/" + fileNameToString); if (!(CheckPanel.this.choosenFile == null) && CheckPanel.this.choosenFile.exists())
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(); this.textField.setText(this.choosenFile.getName());
} result = this.choosenFile;
fileInputStream.close();
byte[] hash = null;
StringBuilder hashString = new StringBuilder();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
hash = messageDigest.digest(bytedirectoryToSave);
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));
}
}
String titre = BUNDLE.getString("CheckPanel.confirmDialogTitle.text");
String yourFile = BUNDLE.getString("CheckPanel.confirmDialogYourString.text");
if (md5String.equals(hashString.toString()))
{
String message = yourFile + fileToCheck1.getName() + " "
+ BUNDLE.getString("CheckPanel.confirmDialogGood.text");
JOptionPane.showMessageDialog(null, message, titre, JOptionPane.INFORMATION_MESSAGE);
} }
else else
{ {
String message = yourFile + fileToCheck1.getName() + " " result = null;
+ BUNDLE.getString("CheckPanel.confirmDialogBad.text");
JOptionPane.showMessageDialog(null, message, titre, JOptionPane.INFORMATION_MESSAGE);
} }
} return result;
/**
*
* @throws IOException
* @throws NoSuchAlgorithmException
*/
final void choosenDirectory() throws IOException, NoSuchAlgorithmException
{
this.choosenFile = CheckDirectorySelector.showSelectorDialog(null, null);
if (this.choosenFile == null)
{
return;
}
this.textField.setText(this.choosenFile.getName());
// } // }
} }

View File

@ -26,3 +26,5 @@ CheckPanel.confirmDialogTitle.text = Check done
CheckPanel.confirmDialogYourString.text = Your File CheckPanel.confirmDialogYourString.text = Your File
CheckPanel.confirmDialogGood.text = has been check and is good CheckPanel.confirmDialogGood.text = has been check and is good
CheckPanel.confirmDialogBad.text = is corrupted CheckPanel.confirmDialogBad.text = is corrupted
BackupPanel.dataMissing.text = Data Missing
BackupPanel.TitleWarning.text = Warning

View File

@ -26,3 +26,5 @@ CheckPanel.confirmDialogTitle.text = V
CheckPanel.confirmDialogYourString.text = Votre fichier CheckPanel.confirmDialogYourString.text = Votre fichier
CheckPanel.confirmDialogGood.text = a été vérifié et ne comporte pas d'erreurs CheckPanel.confirmDialogGood.text = a été vérifié et ne comporte pas d'erreurs
CheckPanel.confirmDialogBad.text = comporte des erreurs CheckPanel.confirmDialogBad.text = comporte des erreurs
BackupPanel.dataMissing.text = Données incomplètes
BackupPanel.TitleWarning.text = Attention

View File

@ -22,69 +22,119 @@ package org.dclermonte.siba.model;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.LinkedList; import java.util.ResourceBundle;
import java.util.List;
import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveException;
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.TarArchiveOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; 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 int pathLength; private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.gui.messages"); //$NON-NLS-1$
public static File backup(final File fileToSave, final String target) throws ArchiveException, IOException public static File backup(final File fileToSave, final String target) throws ArchiveException, IOException
{ {
File result; File result;
if ((fileToSave == null) || (new File(target) == null)) int pathLength = fileToSave.getParentFile().getAbsolutePath().length();
{
return null;
}
pathLength = fileToSave.getParentFile().getAbsolutePath().length();
LocalDateTime date = LocalDateTime.now(); LocalDateTime date = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'H'h'm'mn's's'"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'H'h'm'mn's's'");
String text = date.format(formatter); String textDate = date.format(formatter);
String outputFileNameWithoutExtension = fileToSave.getName() + "-" + text; String outputFileNameWithoutExtension = fileToSave.getName() + "-" + textDate;
result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz"); result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz");
OutputStream gzipOutputStream = new GzipCompressorOutputStream( OutputStream gzipOutputStream = new GzipCompressorOutputStream(
new BufferedOutputStream(new FileOutputStream(result))); new BufferedOutputStream(new FileOutputStream(result)));
TarArchiveOutputStream out = new TarArchiveOutputStream(gzipOutputStream); TarArchiveOutputStream out = new TarArchiveOutputStream(gzipOutputStream);
directoryToSave(fileToSave, out); directoryToSave(fileToSave, out, pathLength);
out.close(); out.close();
md5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5"); md5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5");
//
return result; return result;
} }
public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream) public static boolean check(final File choosenFile) throws IOException, NoSuchAlgorithmException
throws IOException {
boolean result;
FileReader fileReader = new FileReader(choosenFile);
char[] md5 = new char[32];
byte[] md5byte = new byte[32];
char[] fileToCheck = new char[(int) (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 < (choosenFile.length() + 2); index++)
{
fileToCheck[index - 36] = (char) fileReader.read();
fileNameToString = fileNameToString + fileToCheck[index - 36];
}
fileReader.close();
File fileToCheck1 = new File(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);
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));
}
}
if (md5String.equals(hashString.toString()))
{
result = true;
}
else
{
result = false;
}
return result;
}
public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream,
final int pathLength) throws IOException
{ {
for (File file : directory.listFiles()) for (File file : directory.listFiles())
{ {
if (file.isDirectory()) if (file.isDirectory())
{ {
directoryToSave(file, outputStream); directoryToSave(file, outputStream, pathLength);
} }
else else
{ {
@ -100,51 +150,6 @@ public class SibaManager
} }
public static void extract(final File directoryToUntar, final File destination) throws IOException, ArchiveException
{
File file = destination;
try
{
final List<File> untaredFiles = new LinkedList<File>();
InputStream inputStream;
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)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static File md5(final File directoryToSave, final String destination) throws IOException public static File md5(final File directoryToSave, final String destination) throws IOException
{ {
File result; File result;
@ -192,5 +197,4 @@ public class SibaManager
return result; return result;
} }
public GzipCompressorOutputStream gzCompress;
} }