2016-04-19 22:34:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
|
|
|
|
* Didier Clermonté <dclermonte@april.org>
|
|
|
|
*
|
|
|
|
* This file is part of Siba.
|
|
|
|
*
|
|
|
|
* Siba is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
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;
|
2016-04-12 00:41:43 +02:00
|
|
|
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
|
2016-03-27 22:32:35 +02:00
|
|
|
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-18 23:22:33 +02:00
|
|
|
public static int pathLength;
|
2016-04-01 00:08:32 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
public static File backup(final File toto, final String target) throws ArchiveException, IOException
|
|
|
|
{
|
2016-04-19 22:34:25 +02:00
|
|
|
File result;
|
2016-04-23 17:50:13 +02:00
|
|
|
|
2016-04-18 23:22:33 +02:00
|
|
|
pathLength = toto.getParentFile().getAbsolutePath().length();
|
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
/* String outputFileNameWithoutExtension = toto.getName()+"-"+ LocalDateTime.now(); */
|
|
|
|
LocalDateTime date = LocalDateTime.now();
|
2016-04-19 22:34:25 +02:00
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'H'h'm'mn's's'");
|
2016-04-12 00:41:43 +02:00
|
|
|
String text = date.format(formatter);
|
|
|
|
String outputFileNameWithoutExtension = toto.getName() + "-" + text;
|
|
|
|
String outputFileName = outputFileNameWithoutExtension + ".tar";
|
|
|
|
File output = new File(target + "/" + outputFileName);
|
2016-04-19 22:34:25 +02:00
|
|
|
result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz");
|
2016-04-12 00:41:43 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
gzCompress.write(fileTar);
|
|
|
|
gzCompress.close();
|
2016-04-18 23:22:33 +02:00
|
|
|
output.delete();
|
2016-04-19 22:34:25 +02:00
|
|
|
md5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5");
|
2016-04-23 17:50:13 +02:00
|
|
|
|
|
|
|
//
|
2016-04-19 22:34:25 +02:00
|
|
|
return result;
|
2016-04-01 00:08:32 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
|
|
|
public static void directoryToSave(final File directory, final ArchiveOutputStream outputStream) throws IOException
|
2016-04-07 00:54:03 +02:00
|
|
|
{
|
2016-04-12 00:41:43 +02:00
|
|
|
for (File file : directory.listFiles())
|
2016-03-27 22:32:35 +02:00
|
|
|
{
|
2016-04-18 23:22:33 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
|
|
|
directoryToSave(file, outputStream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-18 23:22:33 +02:00
|
|
|
TarArchiveEntry tae = new TarArchiveEntry(file);
|
|
|
|
String pathPartiel = file.getPath().substring(pathLength);
|
|
|
|
tae.setName(pathPartiel);
|
|
|
|
/* outputStream.putArchiveEntry(new TarArchiveEntry(file)); */
|
|
|
|
outputStream.putArchiveEntry(tae);
|
2016-04-12 00:41:43 +02:00
|
|
|
IOUtils.copy(new FileInputStream(file), outputStream);
|
|
|
|
outputStream.closeArchiveEntry();
|
|
|
|
}
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
|
|
|
public static void extract(final File directoryToUntar, final File destination) throws IOException, ArchiveException
|
2016-03-27 22:32:35 +02:00
|
|
|
{
|
2016-04-12 00:41:43 +02:00
|
|
|
File file = destination;
|
2016-03-27 22:32:35 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2016-04-23 17:50:13 +02:00
|
|
|
final List<File> untaredFiles = new LinkedList<File>();
|
|
|
|
InputStream inputStream;
|
2016-03-29 23:25:18 +02:00
|
|
|
inputStream = new FileInputStream(directoryToUntar);
|
2016-04-12 00:41:43 +02:00
|
|
|
|
|
|
|
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)
|
2016-03-27 22:32:35 +02:00
|
|
|
{
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-03-26 23:26:19 +01:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
2016-03-27 22:32:35 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
public static File md5(final File directoryToSave, final String destination) throws IOException
|
|
|
|
{
|
2016-04-23 17:50:13 +02:00
|
|
|
File result;
|
|
|
|
|
|
|
|
result = new File(destination);
|
2016-04-12 00:41:43 +02:00
|
|
|
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();
|
|
|
|
}
|
2016-03-27 22:32:35 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 22:34:25 +02:00
|
|
|
FileWriter fileWriter = new FileWriter(result);
|
2016-04-12 00:41:43 +02:00
|
|
|
fileWriter.write(hashString.toString());
|
2016-04-19 22:34:25 +02:00
|
|
|
String newLine = System.getProperty("line.separator");
|
|
|
|
fileWriter.append(" " + directoryToSave.getName() + newLine);
|
2016-04-12 00:41:43 +02:00
|
|
|
fileWriter.close();
|
2016-04-19 22:34:25 +02:00
|
|
|
fileInputStream.close();
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException e)
|
|
|
|
{
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2016-04-19 22:34:25 +02:00
|
|
|
return result;
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
|
|
|
}
|