/* * Copyright (C) 2016 Didier Clermonté * Copyright (C) 2016 Christian Pierre Momon * * 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 . */ package org.dclermonte.siba.model; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.utils.IOUtils; import org.dclermonte.siba.SibaNullException; public class SibaManager { public static File backup(final File fileToSave, final File target) throws ArchiveException, IOException, SibaNullException { File result; if ((fileToSave == null) || (target == null)) { throw new SibaNullException("Données incomplètes"); } int pathLength = fileToSave.getParentFile().getAbsolutePath().length(); LocalDateTime date = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'H'h'm'mn's's'"); String textDate = date.format(formatter); String outputFileNameWithoutExtension = fileToSave.getName() + "-" + textDate; result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz"); OutputStream gzipOutputStream = new GzipCompressorOutputStream( new BufferedOutputStream(new FileOutputStream(result))); TarArchiveOutputStream out = new TarArchiveOutputStream(gzipOutputStream); directoryToSave(fileToSave, out, pathLength); out.close(); md5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5"); return result; } public static boolean check(final File choosenFile) throws IOException, NoSuchAlgorithmException { boolean result; FileReader fileReader = new FileReader(choosenFile); char[] md5 = new char[32]; byte[] md5byte = new byte[32]; char[] fileToCheck = new char[(int) (choosenFile.length()) - 32]; String md5String = new String(); for (int index = 0; index < 32; index++) { md5[index] = (char) fileReader.read(); md5byte[index] = (byte) md5[index]; md5String = md5String + md5[index]; } fileReader.read(); String fileNameToString = new String(); for (int index = 36; index < (choosenFile.length() + 2); index++) { fileToCheck[index - 36] = (char) fileReader.read(); fileNameToString = fileNameToString + fileToCheck[index - 36]; } fileReader.close(); File fileToCheck1 = new File(choosenFile.getParent() + "/" + fileNameToString); byte[] bytedirectoryToSave = new byte[(int) fileToCheck1.length()]; FileInputStream fileInputStream = new FileInputStream(fileToCheck1); for (int index1 = 0; index1 < (fileToCheck1.length()); index1++) { bytedirectoryToSave[index1] = (byte) fileInputStream.read(); } fileInputStream.close(); byte[] hash = null; StringBuilder hashString = new StringBuilder(); MessageDigest messageDigest = MessageDigest.getInstance("MD5"); hash = messageDigest.digest(bytedirectoryToSave); 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)); } } if (md5String.equals(hashString.toString())) { result = true; } else { result = false; } return result; } public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream, final int pathLength) throws IOException { for (File file : directory.listFiles()) { if (file.isDirectory()) { directoryToSave(file, outputStream, pathLength); } else { TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file); String pathPartiel = file.getPath().substring(pathLength); tarArchiveEntry.setName(pathPartiel); outputStream.putArchiveEntry(tarArchiveEntry); IOUtils.copy(new FileInputStream(file), outputStream); outputStream.closeArchiveEntry(); } } } public static File md5(final File directoryToSave, final String destination) throws IOException { File result; 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; } }