2016-06-07 22:20:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
|
|
|
|
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2016-06-02 23:18:20 +02:00
|
|
|
package org.dclermonte.siba.model;
|
|
|
|
|
2016-06-19 12:09:10 +02:00
|
|
|
import java.io.BufferedReader;
|
2016-06-02 23:18:20 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileReader;
|
2016-06-22 22:54:56 +02:00
|
|
|
import java.io.FileWriter;
|
2016-06-02 23:18:20 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
2016-06-14 16:21:19 +02:00
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
2016-06-02 23:18:20 +02:00
|
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
|
|
|
2016-06-19 22:37:05 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Didier Clermonté (dclermonte@april.org)
|
|
|
|
*
|
|
|
|
*/
|
2016-06-02 23:18:20 +02:00
|
|
|
public class SibaUtils
|
|
|
|
{
|
2016-06-25 00:37:01 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This method Generate the file with MD5.
|
|
|
|
*
|
2016-06-26 12:40:21 +02:00
|
|
|
* @param inputFile
|
2016-06-25 00:37:01 +02:00
|
|
|
* @param target
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
*/
|
2016-06-26 12:40:21 +02:00
|
|
|
public static File createMD5File(final File inputFile, final String target)
|
2016-06-25 00:37:01 +02:00
|
|
|
throws IOException, NoSuchAlgorithmException
|
2016-06-22 22:54:56 +02:00
|
|
|
{
|
|
|
|
File result;
|
|
|
|
|
2016-06-26 12:40:21 +02:00
|
|
|
String md5 = SibaUtils.md5(inputFile);
|
|
|
|
String fileName = inputFile.getName();
|
2016-06-25 00:37:01 +02:00
|
|
|
result = createMD5File(md5, fileName, target);
|
2016-06-22 22:54:56 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-04 16:02:01 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param md5
|
|
|
|
* @param fileName
|
|
|
|
* @param target
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2016-06-25 00:37:01 +02:00
|
|
|
public static File createMD5File(final String md5, final String fileName, final String target) throws IOException
|
|
|
|
{
|
|
|
|
File result;
|
2016-06-27 15:27:45 +02:00
|
|
|
FileWriter out = null;
|
2016-06-25 00:37:01 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
result = new File(target);
|
2016-06-27 15:27:45 +02:00
|
|
|
out = new FileWriter(result);
|
|
|
|
out.write(md5);
|
2016-06-25 00:37:01 +02:00
|
|
|
String newLine = System.getProperty("line.separator");
|
2016-06-27 15:27:45 +02:00
|
|
|
out.append(" ");
|
|
|
|
out.append(fileName);
|
|
|
|
out.append(newLine);
|
|
|
|
out.close();
|
2016-06-25 00:37:01 +02:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2016-06-27 15:27:45 +02:00
|
|
|
IOUtils.closeQuietly(out);
|
2016-06-25 00:37:01 +02:00
|
|
|
}
|
2016-06-27 15:27:45 +02:00
|
|
|
|
2016-06-25 00:37:01 +02:00
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-02 23:18:20 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param choosenFile
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public static String loadMD5Sum(final File choosenFile) throws IOException
|
|
|
|
{
|
2016-06-19 12:09:10 +02:00
|
|
|
String result;
|
2016-06-26 12:40:21 +02:00
|
|
|
FileReader in = null;
|
2016-06-25 00:37:01 +02:00
|
|
|
try
|
2016-06-02 23:18:20 +02:00
|
|
|
{
|
2016-06-25 00:37:01 +02:00
|
|
|
if (choosenFile.exists() && (choosenFile.length() > 32))
|
|
|
|
{
|
2016-06-26 12:40:21 +02:00
|
|
|
in = new FileReader(choosenFile);
|
|
|
|
BufferedReader bufferedReader = new BufferedReader(in);
|
2016-06-25 00:37:01 +02:00
|
|
|
String line = bufferedReader.readLine();
|
|
|
|
result = line.substring(0, 32);
|
2016-06-26 12:40:21 +02:00
|
|
|
in.close();
|
2016-06-25 00:37:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = "";
|
|
|
|
}
|
2016-06-02 23:18:20 +02:00
|
|
|
}
|
2016-06-25 00:37:01 +02:00
|
|
|
finally
|
2016-06-21 22:14:02 +02:00
|
|
|
{
|
2016-06-26 12:40:21 +02:00
|
|
|
IOUtils.closeQuietly(in);
|
2016-06-21 22:14:02 +02:00
|
|
|
}
|
2016-06-02 23:18:20 +02:00
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method calculate the MD5 itself.
|
|
|
|
*
|
|
|
|
* @param input
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
*/
|
|
|
|
public static String md5(final File input) throws IOException, NoSuchAlgorithmException
|
|
|
|
{
|
|
|
|
String result;
|
2016-06-19 22:37:05 +02:00
|
|
|
|
2016-06-02 23:18:20 +02:00
|
|
|
StringBuilder hashString = new StringBuilder();
|
|
|
|
FileInputStream fileInputStream = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
byte[] byteInput = new byte[(int) input.length()];
|
|
|
|
fileInputStream = new FileInputStream(input);
|
|
|
|
for (int index1 = 0; index1 < input.length(); index1++)
|
|
|
|
{
|
|
|
|
byteInput[index1] = (byte) fileInputStream.read();
|
|
|
|
}
|
|
|
|
byte[] hash = null;
|
|
|
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
|
|
|
hash = messageDigest.digest(byteInput);
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileInputStream.close();
|
|
|
|
|
|
|
|
result = hashString.toString();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
IOUtils.closeQuietly(fileInputStream);
|
|
|
|
}
|
2016-06-19 22:37:05 +02:00
|
|
|
|
2016-06-02 23:18:20 +02:00
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
2016-06-14 16:21:19 +02:00
|
|
|
|
2016-06-19 12:09:10 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param choosenFile
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public static File readFileNameToCheck(final File choosenFile) throws IOException
|
|
|
|
{
|
|
|
|
File result;
|
2016-06-26 12:40:21 +02:00
|
|
|
FileReader in = null;
|
2016-06-25 00:37:01 +02:00
|
|
|
try
|
2016-06-19 12:09:10 +02:00
|
|
|
{
|
2016-06-25 00:37:01 +02:00
|
|
|
if (choosenFile.exists() && (choosenFile.length() > 32))
|
2016-06-19 12:09:10 +02:00
|
|
|
{
|
2016-06-26 12:40:21 +02:00
|
|
|
in = new FileReader(choosenFile);
|
|
|
|
BufferedReader bufferedReader = new BufferedReader(in);
|
2016-06-25 00:37:01 +02:00
|
|
|
String line = bufferedReader.readLine();
|
|
|
|
String fileNameToString = new String();
|
|
|
|
fileNameToString = line.substring(33);
|
2016-06-26 12:40:21 +02:00
|
|
|
in.close();
|
2016-06-25 00:37:01 +02:00
|
|
|
if (choosenFile.isAbsolute())
|
|
|
|
{
|
|
|
|
result = new File(choosenFile.getParent() + "/" + fileNameToString);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String path = System.getProperty("user.dir");
|
|
|
|
result = new File(path + "/" + fileNameToString);
|
|
|
|
}
|
2016-06-19 12:09:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-25 00:37:01 +02:00
|
|
|
result = null;
|
2016-06-19 12:09:10 +02:00
|
|
|
}
|
2016-06-21 22:14:02 +02:00
|
|
|
}
|
2016-06-25 00:37:01 +02:00
|
|
|
finally
|
2016-06-21 22:14:02 +02:00
|
|
|
{
|
2016-06-26 12:40:21 +02:00
|
|
|
IOUtils.closeQuietly(in);
|
2016-06-21 22:14:02 +02:00
|
|
|
}
|
2016-06-19 22:37:05 +02:00
|
|
|
//
|
2016-06-19 12:09:10 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:05:50 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param fileToSave
|
|
|
|
* @param outputStream
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public static void tarDirectoryTree(final File fileToSave, final TarArchiveOutputStream outputStream)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
int pathLength = fileToSave.getParentFile().getAbsolutePath().length();
|
|
|
|
tarDirectoryTree(fileToSave, outputStream, pathLength);
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:21:19 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This method generates ArchiveEntry.
|
|
|
|
*
|
|
|
|
* @param directory
|
|
|
|
* @param outputStream
|
|
|
|
* @param pathLength
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public static void tarDirectoryTree(final File directory, final TarArchiveOutputStream outputStream,
|
|
|
|
final int pathLength) throws IOException
|
|
|
|
{
|
|
|
|
for (File file : directory.listFiles())
|
|
|
|
{
|
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
|
|
|
if (file.listFiles().length == 0)
|
|
|
|
{
|
|
|
|
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file);
|
|
|
|
String pathPartiel = file.getPath().substring(pathLength);
|
|
|
|
tarArchiveEntry.setName(pathPartiel);
|
|
|
|
outputStream.putArchiveEntry(tarArchiveEntry);
|
|
|
|
outputStream.closeArchiveEntry();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tarDirectoryTree(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();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 23:18:20 +02:00
|
|
|
}
|