Siba/src/siba/model/SibaModel.java

99 lines
3.1 KiB
Java
Raw Normal View History

2016-03-26 23:26:19 +01:00
package siba.model;
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;
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-26 23:26:19 +01:00
public class SibaModel
{
2016-03-27 22:32:35 +02:00
public static File backup(File repertoireAsauvegarder){
File sauvegarde = new File("");
return sauvegarde;
}
public static File md5(File repertoireAsauvegarder) throws IOException{
File sauvegarde = new File(repertoireAsauvegarder.getName()+"md5");
try
{ byte[] byterepertoireAsauvegarder = repertoireAsauvegarder.toString().getBytes() ;
byte[] hash = null;
MessageDigest md = MessageDigest.getInstance("MD5");
hash = md.digest(byterepertoireAsauvegarder);
StringBuilder hashString = new StringBuilder();
for (int i = 0; i < hash.length; i++)
{
String hex = Integer.toHexString(hash[i]);
if (hex.length() == 1)
{
hashString.append('0');
hashString.append(hex.charAt(hex.length() - 1));
}
else
hashString.append(hex.substring(hex.length() - 2));
}
FileWriter ffw = new FileWriter(sauvegarde);
ffw.write(hashString.toString());
ffw.close();
}
catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return sauvegarde;
}
public static void extract(File repertoireAdecompresser, File destination) throws IOException, ArchiveException{
File file = destination;
final List<File> untaredFiles = new LinkedList<File>();
InputStream is;
try
{
is = new FileInputStream(repertoireAdecompresser);
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
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