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

106 lines
3.6 KiB
Java
Raw Normal View History

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;
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-03-27 22:32:35 +02:00
2016-03-29 23:25:18 +02:00
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");
ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out);
os.putArchiveEntry(new TarArchiveEntry(toto));
IOUtils.copy(new FileInputStream(toto), os);
os.closeArchiveEntry();
os.close();
return output;
2016-03-27 22:32:35 +02:00
}
2016-03-29 23:25:18 +02:00
public static File md5(File directoryToSave) throws IOException{
File saved = new File(directoryToSave.getName()+"md5");
2016-03-27 22:32:35 +02:00
try
2016-03-29 23:25:18 +02:00
{ byte[] bytedirectoryToSave = directoryToSave.toString().getBytes() ;
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());
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