Siba/src/org/dclermonte/siba/model/SibaManager.java

175 lines
5.6 KiB
Java

package org.dclermonte.siba.model;
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;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
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.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils;
public class SibaManager
{
public static int pathLength;
public static File backup(final File toto, final String target) throws ArchiveException, IOException
{
pathLength = toto.getParentFile().getAbsolutePath().length();
/* 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 + ".tar";
File output = new File(target + "/" + outputFileName);
File output1 = new File(target + "/" + outputFileNameWithoutExtension + ".tgz");
final OutputStream out = new FileOutputStream(target + "/" + outputFileName);
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();
output.delete();
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
{
TarArchiveEntry tae = new TarArchiveEntry(file);
String pathPartiel = file.getPath().substring(pathLength);
tae.setName(pathPartiel);
/* outputStream.putArchiveEntry(new TarArchiveEntry(file)); */
outputStream.putArchiveEntry(tae);
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
{
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
{
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;
}
}