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

192 lines
6.4 KiB
Java

/*
* 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/>.
*/
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
{
File result;
pathLength = toto.getParentFile().getAbsolutePath().length();
/* String outputFileNameWithoutExtension = toto.getName()+"-"+ LocalDateTime.now(); */
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'H'h'm'mn's's'");
String text = date.format(formatter);
String outputFileNameWithoutExtension = toto.getName() + "-" + text;
String outputFileName = outputFileNameWithoutExtension + ".tar";
File output = new File(target + "/" + outputFileName);
result = 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);
gzCompress.write(fileTar);
gzCompress.close();
output.delete();
md5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5");
return result;
}
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 result = 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(result);
fileWriter.write(hashString.toString());
String newLine = System.getProperty("line.separator");
fileWriter.append(" " + directoryToSave.getName() + newLine);
fileWriter.close();
fileInputStream.close();
}
catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}