Completed BackupPanel.

This commit is contained in:
Didier Clermonté 2016-04-01 00:08:32 +02:00
parent 0a21ea5099
commit 08acb51455
2 changed files with 41 additions and 21 deletions

View File

@ -26,7 +26,7 @@ import org.dclermonte.siba.model.*;
public class BackupPanel extends JPanel public class BackupPanel extends JPanel
{ {
private JTextField textField; private JTextField textField;
private JTextField textField_1; public JTextField textField_1;
/** /**
* Create the panel. * Create the panel.
@ -50,9 +50,11 @@ public class BackupPanel extends JPanel
}); });
setLayout(new FormLayout(new ColumnSpec[] { setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("center:2dlu"), ColumnSpec.decode("center:2dlu"),
new ColumnSpec(ColumnSpec.LEFT, Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true), Sizes.constant("52dlu", true)), 1), FormSpecs.DEFAULT_COLSPEC,
new ColumnSpec(ColumnSpec.LEFT, Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true), Sizes.constant("90dlu", true)), 1), FormSpecs.RELATED_GAP_COLSPEC,
ColumnSpec.decode("left:30dlu"),}, ColumnSpec.decode("default:grow"),
FormSpecs.RELATED_GAP_COLSPEC,
FormSpecs.DEFAULT_COLSPEC,},
new RowSpec[] { new RowSpec[] {
RowSpec.decode("5dlu"), RowSpec.decode("5dlu"),
RowSpec.decode("25px"), RowSpec.decode("25px"),
@ -61,17 +63,19 @@ public class BackupPanel extends JPanel
FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC,
FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,
FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC,})); FormSpecs.DEFAULT_ROWSPEC,}));
JLabel lblNewLabel = new JLabel("Répertoire à sauvegarder"); JLabel lblNewLabel = new JLabel("Source Directory:");
lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT); lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel.setVerticalAlignment(SwingConstants.TOP); lblNewLabel.setVerticalAlignment(SwingConstants.TOP);
add(lblNewLabel, "2, 2, left, center"); add(lblNewLabel, "2, 2, left, center");
textField = new JTextField(); textField = new JTextField();
add(textField, "3, 2, fill, default"); add(textField, "4, 2, fill, default");
textField.setColumns(10); textField.setColumns(10);
add(btnNewButton, "4, 2, left, top"); add(btnNewButton, "6, 2, left, top");
JButton btnNewButton_1 = new JButton("..."); JButton btnNewButton_1 = new JButton("...");
btnNewButton_1.addMouseListener(new MouseAdapter() { btnNewButton_1.addMouseListener(new MouseAdapter() {
@ -87,23 +91,23 @@ public class BackupPanel extends JPanel
} }
}); });
JLabel lblRpertoireDeDestination = new JLabel("Répertoire de destination"); JLabel lblRpertoireDeDestination = new JLabel("Target Directory:");
lblRpertoireDeDestination.setVerticalAlignment(SwingConstants.TOP); lblRpertoireDeDestination.setVerticalAlignment(SwingConstants.TOP);
lblRpertoireDeDestination.setHorizontalAlignment(SwingConstants.LEFT); lblRpertoireDeDestination.setHorizontalAlignment(SwingConstants.LEFT);
add(lblRpertoireDeDestination, "2, 4, left, default"); add(lblRpertoireDeDestination, "2, 4, left, default");
textField_1 = new JTextField(); textField_1 = new JTextField();
add(textField_1, "3, 4, fill, default"); add(textField_1, "4, 4, fill, default");
textField_1.setColumns(10); textField_1.setColumns(10);
btnNewButton_1.setHorizontalAlignment(SwingConstants.RIGHT); btnNewButton_1.setHorizontalAlignment(SwingConstants.RIGHT);
add(btnNewButton_1, "4, 4, left, top"); add(btnNewButton_1, "6, 4, left, top");
JButton btnNewButton_2 = new JButton("Backup"); JButton btnNewButton_2 = new JButton("Backup");
btnNewButton_2.addActionListener(new ActionListener() { btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try try
{ {
SibaManager.backup(new File("/home/papou/logique_2/notes-de-cours2-S3.1.pdf")); SibaManager.backup(new File(textField.getText()),textField_1.getText());
} }
catch (ArchiveException | IOException e1) catch (ArchiveException | IOException e1)
{ {
@ -112,7 +116,7 @@ public class BackupPanel extends JPanel
} }
} }
}); });
add(btnNewButton_2, "3, 8"); add(btnNewButton_2, "4, 10");
} }

View File

@ -10,6 +10,7 @@ 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.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -19,23 +20,38 @@ 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.utils.IOUtils; import org.apache.commons.compress.utils.IOUtils;
import org.dclermonte.siba.gui.BackupPanel;
public class SibaManager public class SibaManager
{ {
public static File backup(File toto) throws ArchiveException, IOException{ public static File backup(File toto,String destination) throws ArchiveException, IOException{
File output = new File("/home/papou/output.tgz"); String outputFileNameWithoutExtension = toto.getName()+"-"+ LocalDateTime.now();
final OutputStream out = new FileOutputStream("/home/papou/output.tgz"); 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); ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out);
os.putArchiveEntry(new TarArchiveEntry(toto)); directoryToSave(toto,os);
IOUtils.copy(new FileInputStream(toto), os);
os.closeArchiveEntry();
os.close(); os.close();
md5(output,destination+"/"+outputFileNameWithoutExtension+".md5");
return output; return output;
} }
public static File md5(File directoryToSave) throws IOException{ public static void directoryToSave(File directory,ArchiveOutputStream outputStream) throws IOException{
File saved = new File(directoryToSave.getName()+"md5"); 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 try
{ byte[] bytedirectoryToSave = directoryToSave.toString().getBytes() ; { byte[] bytedirectoryToSave = directoryToSave.toString().getBytes() ;
byte[] hash = null; byte[] hash = null;