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

198 lines
5.1 KiB
Java

/*
* 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/>.
*/
package org.dclermonte.siba.model;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
public class SibaUtils
{
/**
*
* @param choosenFile
* @return
* @throws IOException
*/
public static File loadFileToCheck(final File choosenFile) throws IOException
{
File result = null;
if (choosenFile.exists() && (choosenFile.length() > 32))
{
FileReader fileReader = new FileReader(choosenFile);
// BufferedReader bufferedReader = new BufferedReader(fileReader);
// String line = bufferedReader.readLine();
char[] fileToCheck = new char[(int) (choosenFile.length()) - 32];
fileReader.read();
fileReader.skip(32);
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();
if (choosenFile.isAbsolute())
{
result = new File(choosenFile.getParent() + "/" + fileNameToString);
}
else
{
String path = System.getProperty("user.dir");
result = new File(path + "/" + fileNameToString);
}
}
//
return result;
}
/**
*
* @param choosenFile
* @return
* @throws IOException
*/
public static String loadMD5Sum(final File choosenFile) throws IOException
{
String result = "";
if (choosenFile.exists() && (choosenFile.length() > 32))
{
FileReader fileReader = new FileReader(choosenFile);
char[] md5 = new char[32];
byte[] md5byte = new byte[32];
for (int index = 0; index < 32; index++)
{
md5[index] = (char) fileReader.read();
md5byte[index] = (byte) md5[index];
result = result + md5[index];
}
fileReader.close();
}
//
//
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;
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);
}
//
return result;
}
/**
*
* 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();
}
}
}
}