2016-03-29 23:25:18 +02:00
|
|
|
package org.dclermonte.siba.model;
|
2016-03-26 23:26:19 +01:00
|
|
|
|
2016-03-27 22:32:35 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2016-04-01 00:08:32 +02:00
|
|
|
import java.time.LocalDateTime;
|
2016-04-07 00:54:03 +02:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2016-03-27 22:32:35 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.apache.commons.compress.archivers.ArchiveException;
|
2016-03-29 23:25:18 +02:00
|
|
|
import org.apache.commons.compress.archivers.ArchiveOutputStream;
|
2016-03-27 22:32:35 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2016-03-29 23:25:18 +02:00
|
|
|
public class SibaManager
|
2016-03-26 23:26:19 +01:00
|
|
|
{
|
2016-04-01 00:08:32 +02:00
|
|
|
|
|
|
|
public static File backup(File toto,String destination) throws ArchiveException, IOException{
|
2016-04-07 00:54:03 +02:00
|
|
|
/* 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;
|
2016-04-01 00:08:32 +02:00
|
|
|
String outputFileName = outputFileNameWithoutExtension + ".tgz";
|
|
|
|
File output = new File(destination+"/"+outputFileName);
|
|
|
|
|
2016-04-07 00:54:03 +02:00
|
|
|
|
2016-04-01 00:08:32 +02:00
|
|
|
final OutputStream out = new FileOutputStream(destination+"/"+outputFileName);
|
2016-03-29 23:25:18 +02:00
|
|
|
ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out);
|
2016-04-01 00:08:32 +02:00
|
|
|
directoryToSave(toto,os);
|
2016-03-29 23:25:18 +02:00
|
|
|
os.close();
|
2016-04-01 00:08:32 +02:00
|
|
|
md5(output,destination+"/"+outputFileNameWithoutExtension+".md5");
|
2016-03-29 23:25:18 +02:00
|
|
|
return output;
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-04-01 00:08:32 +02:00
|
|
|
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);
|
2016-03-27 22:32:35 +02:00
|
|
|
try
|
2016-04-07 00:54:03 +02:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-27 22:32:35 +02:00
|
|
|
byte[] hash = null;
|
2016-03-29 23:25:18 +02:00
|
|
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
|
|
|
hash = messageDigest.digest(bytedirectoryToSave);
|
2016-03-27 22:32:35 +02:00
|
|
|
StringBuilder hashString = new StringBuilder();
|
2016-03-29 23:25:18 +02:00
|
|
|
for (int index = 0; index < hash.length; index++)
|
2016-03-27 22:32:35 +02:00
|
|
|
{
|
2016-03-29 23:25:18 +02:00
|
|
|
String hex = Integer.toHexString(hash[index]);
|
2016-03-27 22:32:35 +02:00
|
|
|
if (hex.length() == 1)
|
|
|
|
{
|
|
|
|
hashString.append('0');
|
|
|
|
hashString.append(hex.charAt(hex.length() - 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
hashString.append(hex.substring(hex.length() - 2));
|
|
|
|
}
|
2016-03-29 23:25:18 +02:00
|
|
|
FileWriter fileWriter = new FileWriter(saved);
|
|
|
|
fileWriter.write(hashString.toString());
|
2016-04-07 00:54:03 +02:00
|
|
|
fileWriter.append(" " + directoryToSave.getName());
|
2016-03-29 23:25:18 +02:00
|
|
|
fileWriter.close();
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException e)
|
|
|
|
{
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2016-03-29 23:25:18 +02:00
|
|
|
return saved;
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-03-29 23:25:18 +02:00
|
|
|
public static void extract(File directoryToUntar, File destination) throws IOException, ArchiveException{
|
2016-03-27 22:32:35 +02:00
|
|
|
File file = destination;
|
|
|
|
|
|
|
|
final List<File> untaredFiles = new LinkedList<File>();
|
2016-03-29 23:25:18 +02:00
|
|
|
InputStream inputStream;
|
2016-03-27 22:32:35 +02:00
|
|
|
try
|
|
|
|
{
|
2016-03-29 23:25:18 +02:00
|
|
|
inputStream = new FileInputStream(directoryToUntar);
|
2016-03-27 22:32:35 +02:00
|
|
|
|
|
|
|
|
2016-03-29 23:25:18 +02:00
|
|
|
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream);
|
2016-03-27 22:32:35 +02:00
|
|
|
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();
|
|
|
|
}
|
2016-03-26 23:26:19 +01:00
|
|
|
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-03-26 23:26:19 +01:00
|
|
|
}
|
2016-03-27 22:32:35 +02:00
|
|
|
|
|
|
|
|