Modified SibaManager and SibaUtils

This commit is contained in:
Didier Clermonté 2016-06-14 16:21:19 +02:00
parent 2b4f5ae616
commit 598ba9ade1
2 changed files with 93 additions and 92 deletions

View File

@ -21,9 +21,7 @@ package org.dclermonte.siba.model;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -32,7 +30,6 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.IOUtils;
@ -104,9 +101,9 @@ public class SibaManager
gzipOutputStream = new GzipCompressorOutputStream( gzipOutputStream = new GzipCompressorOutputStream(
new BufferedOutputStream(new FileOutputStream(result))); new BufferedOutputStream(new FileOutputStream(result)));
out = new TarArchiveOutputStream(gzipOutputStream); out = new TarArchiveOutputStream(gzipOutputStream);
directoryToSave(fileToSave, out, pathLength); SibaUtils.tarDirectoryTree(fileToSave, out, pathLength);
out.close(); out.close();
fileWithMD5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5"); createMD5File(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5");
} }
} }
catch (IOException ioExceptionBackup) catch (IOException ioExceptionBackup)
@ -140,7 +137,7 @@ public class SibaManager
try try
{ {
String md5String = SibaUtils.loadMD5Sum(choosenFile); String md5String = SibaUtils.loadMD5Sum(choosenFile);
File fileToCheck1 = loadFileToCheck(choosenFile); File fileToCheck1 = SibaUtils.loadFileToCheck(choosenFile);
if (StringUtils.equals(md5String, SibaUtils.md5(fileToCheck1))) if (StringUtils.equals(md5String, SibaUtils.md5(fileToCheck1)))
{ {
result = true; result = true;
@ -162,50 +159,6 @@ public class SibaManager
return result; return result;
} }
/**
*
* This method generates ArchiveEntry.
*
* @param directory
* @param outputStream
* @param pathLength
* @throws IOException
*/
public static void directoryToSave(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
{
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();
}
}
}
/** /**
* *
* This method Generate the file with MD5. * This method Generate the file with MD5.
@ -216,7 +169,7 @@ public class SibaManager
* @throws IOException * @throws IOException
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
public static File fileWithMD5(final File directoryToSave, final String destination) public static File createMD5File(final File directoryToSave, final String destination)
throws IOException, NoSuchAlgorithmException throws IOException, NoSuchAlgorithmException
{ {
File result; File result;
@ -225,51 +178,13 @@ public class SibaManager
FileWriter fileWriter = new FileWriter(result); FileWriter fileWriter = new FileWriter(result);
fileWriter.write(SibaUtils.md5(directoryToSave)); fileWriter.write(SibaUtils.md5(directoryToSave));
String newLine = System.getProperty("line.separator"); String newLine = System.getProperty("line.separator");
fileWriter.append(" " + directoryToSave.getName() + newLine); fileWriter.append(" ");
fileWriter.append(directoryToSave.getName());
fileWriter.append(newLine);
fileWriter.close(); fileWriter.close();
// //
return result; return result;
} }
/**
*
* @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;
}
} }

View File

@ -26,10 +26,52 @@ import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; 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; import org.apache.commons.compress.utils.IOUtils;
public class SibaUtils 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 * @param choosenFile
@ -108,4 +150,48 @@ public class SibaUtils
// //
return result; 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();
}
}
}
} }