diff --git a/src/org/dclermonte/siba/gui/BackupPanel.java b/src/org/dclermonte/siba/gui/BackupPanel.java index 17578ff..bd5aab3 100644 --- a/src/org/dclermonte/siba/gui/BackupPanel.java +++ b/src/org/dclermonte/siba/gui/BackupPanel.java @@ -26,7 +26,7 @@ import org.dclermonte.siba.model.*; public class BackupPanel extends JPanel { private JTextField textField; - private JTextField textField_1; + public JTextField textField_1; /** * Create the panel. @@ -50,9 +50,11 @@ public class BackupPanel extends JPanel }); setLayout(new FormLayout(new ColumnSpec[] { ColumnSpec.decode("center:2dlu"), - new ColumnSpec(ColumnSpec.LEFT, Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true), Sizes.constant("52dlu", true)), 1), - new ColumnSpec(ColumnSpec.LEFT, Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true), Sizes.constant("90dlu", true)), 1), - ColumnSpec.decode("left:30dlu"),}, + 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"), @@ -61,17 +63,19 @@ public class BackupPanel extends JPanel 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("Répertoire à sauvegarder"); + JLabel lblNewLabel = new JLabel("Source Directory:"); lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT); lblNewLabel.setVerticalAlignment(SwingConstants.TOP); add(lblNewLabel, "2, 2, left, center"); textField = new JTextField(); - add(textField, "3, 2, fill, default"); + add(textField, "4, 2, fill, default"); textField.setColumns(10); - add(btnNewButton, "4, 2, left, top"); + add(btnNewButton, "6, 2, left, top"); JButton btnNewButton_1 = new JButton("..."); 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.setHorizontalAlignment(SwingConstants.LEFT); add(lblRpertoireDeDestination, "2, 4, left, default"); textField_1 = new JTextField(); - add(textField_1, "3, 4, fill, default"); + add(textField_1, "4, 4, fill, default"); textField_1.setColumns(10); 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"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { 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) { @@ -112,7 +116,7 @@ public class BackupPanel extends JPanel } } }); - add(btnNewButton_2, "3, 8"); + add(btnNewButton_2, "4, 10"); } diff --git a/src/org/dclermonte/siba/model/SibaManager.java b/src/org/dclermonte/siba/model/SibaManager.java index ce6a94a..06d61ff 100644 --- a/src/org/dclermonte/siba/model/SibaManager.java +++ b/src/org/dclermonte/siba/model/SibaManager.java @@ -10,6 +10,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.time.LocalDateTime; import java.util.LinkedList; 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.TarArchiveInputStream; import org.apache.commons.compress.utils.IOUtils; +import org.dclermonte.siba.gui.BackupPanel; public class SibaManager { - -public static File backup(File toto) throws ArchiveException, IOException{ - File output = new File("/home/papou/output.tgz"); - final OutputStream out = new FileOutputStream("/home/papou/output.tgz"); + +public static File backup(File toto,String destination) throws ArchiveException, IOException{ + String outputFileNameWithoutExtension = toto.getName()+"-"+ LocalDateTime.now(); + 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); - os.putArchiveEntry(new TarArchiveEntry(toto)); - IOUtils.copy(new FileInputStream(toto), os); - os.closeArchiveEntry(); + directoryToSave(toto,os); os.close(); + md5(output,destination+"/"+outputFileNameWithoutExtension+".md5"); return output; } -public static File md5(File directoryToSave) throws IOException{ - File saved = new File(directoryToSave.getName()+"md5"); +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 = directoryToSave.toString().getBytes() ; byte[] hash = null;