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.utils.IOUtils; public class SibaManager { public static File backup(File toto,String destination) throws ArchiveException, IOException{ /* 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 + ".tgz"; File output = new File(destination+"/"+outputFileName); final OutputStream out = new FileOutputStream(destination+"/"+outputFileName); ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out); directoryToSave(toto,os); os.close(); md5(output,destination+"/"+outputFileNameWithoutExtension+".md5"); return output; } public static void directoryToSave(File directory,ArchiveOutputStream outputStream) throws IOException{ for(File file:directory.listFiles()){ if (file.isDirectory()){ directoryToSave(file,outputStream); }else{ outputStream.putArchiveEntry(new TarArchiveEntry(file)); IOUtils.copy(new FileInputStream(file), outputStream); outputStream.closeArchiveEntry(); } } } public static File md5(File directoryToSave,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; } public static void extract(File directoryToUntar, File destination) throws IOException, ArchiveException{ File file = destination; final List untaredFiles = new LinkedList(); 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(); } } }