From b44a4023b7399c0ab2cb1ba6e6603f29787e12f1 Mon Sep 17 00:00:00 2001 From: Rodrigo Alves Vieira Date: Fri, 3 May 2013 15:00:27 -0300 Subject: [PATCH] Working on the documentation for the extracor package files. Related to #20. --- .../groundhog/extractor/DefaultExtractor.java | 7 ++- .../extractor/DefaultUncompressor.java | 8 +++- .../ufpe/cin/groundhog/extractor/Formats.java | 2 +- .../groundhog/extractor/RarUncompressor.java | 44 +++++++++++++++---- .../UncompressNotSupportedException.java | 7 ++- .../br/ufpe/cin/groundhog/util/FileUtil.java | 4 +- 6 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/java/main/br/ufpe/cin/groundhog/extractor/DefaultExtractor.java b/src/java/main/br/ufpe/cin/groundhog/extractor/DefaultExtractor.java index 2740f34..31cd53e 100644 --- a/src/java/main/br/ufpe/cin/groundhog/extractor/DefaultExtractor.java +++ b/src/java/main/br/ufpe/cin/groundhog/extractor/DefaultExtractor.java @@ -36,9 +36,14 @@ public static DefaultExtractor getInstance() { } private DefaultExtractor() { - } + /** + * Recursively extract files within a root file to a specified destination folder + * @param root the main file containing all the files to be extracted + * @param next + * @param destinationFolder the destination folder to which the extracted files will be moved to + */ private void recursiveExtract(final File root, File next, final File destinationFolder) { ExecutorService executor = Executors.newFixedThreadPool(Config.MAX_NUMBER_OF_THREADS); File[] subFiles = next.listFiles(); diff --git a/src/java/main/br/ufpe/cin/groundhog/extractor/DefaultUncompressor.java b/src/java/main/br/ufpe/cin/groundhog/extractor/DefaultUncompressor.java index ad55e46..cff15bd 100644 --- a/src/java/main/br/ufpe/cin/groundhog/extractor/DefaultUncompressor.java +++ b/src/java/main/br/ufpe/cin/groundhog/extractor/DefaultUncompressor.java @@ -21,9 +21,15 @@ public static DefaultUncompressor getInstance() { } private DefaultUncompressor() { - } + /** + * The compressed file extractor method that takes a compressed file and a destination folder + * and extracts the given file according to its compression format. The supported formats are declared + * in the formats class. + * @param file the compressed file to be extracted + * @param destinationFolder the folder to which the extracted file will be moved to + */ public void uncompress(File file, File destinationFolder) { String name = file.getName(); try { diff --git a/src/java/main/br/ufpe/cin/groundhog/extractor/Formats.java b/src/java/main/br/ufpe/cin/groundhog/extractor/Formats.java index 052c780..4394942 100644 --- a/src/java/main/br/ufpe/cin/groundhog/extractor/Formats.java +++ b/src/java/main/br/ufpe/cin/groundhog/extractor/Formats.java @@ -6,7 +6,7 @@ /** * This class verifies if a given file format is supported to extraction * - * @author ghlp + * @author gustavopinto */ public class Formats { private static Formats instance; diff --git a/src/java/main/br/ufpe/cin/groundhog/extractor/RarUncompressor.java b/src/java/main/br/ufpe/cin/groundhog/extractor/RarUncompressor.java index 9ecef8f..bdfeb5c 100644 --- a/src/java/main/br/ufpe/cin/groundhog/extractor/RarUncompressor.java +++ b/src/java/main/br/ufpe/cin/groundhog/extractor/RarUncompressor.java @@ -13,16 +13,19 @@ import com.github.junrar.rarfile.FileHeader; /** - * - * @author gustavopinto, rodrigoalvesvieira + * @author fjsj, gustavopinto, rodrigoalvesvieira * Adapted from: https://github.com/edmund-wagner/junrar/blob/6f32323c983015d96c64084418793853f514b519/testutil/src/main/java/de/innosystec/unrar/testutil/ExtractArchive.java * Original author: Edmund Wagner - * */ public class RarUncompressor { private static Log logger = LogFactory.getLog(RarUncompressor.class .getName()); + /** + * Extracts an archive file to a specified destination directory + * @param archive the archive file to be extracted + * @param destination the directory to which the extracted file will be moved to + */ public static void extract(File archive, File destination) { destination.mkdirs(); Archive arch = null; @@ -35,7 +38,7 @@ public static void extract(File archive, File destination) { } if (arch != null) { if (arch.isEncrypted()) { - logger.warn("archive is encrypted cannot extreact"); + logger.warn("archive is encrypted, cannot perform extraction"); return; } FileHeader fh = null; @@ -45,7 +48,7 @@ public static void extract(File archive, File destination) { break; } if (fh.isEncrypted()) { - logger.warn("file is encrypted cannot extract: " + logger.warn("file is encrypted cannot be extracted: " + fh.getFileNameString()); continue; } @@ -68,6 +71,12 @@ public static void extract(File archive, File destination) { } } + /** + * Creates a file in a specified destination directory + * @param fh the file to be created + * @param destination the destination to which the newly created file will be moved to + * @return + */ private static File createFile(FileHeader fh, File destination) { File f = null; String name = null; @@ -87,6 +96,13 @@ private static File createFile(FileHeader fh, File destination) { return f; } + /** + * Creates a file in a specified destination folder + * @param destination the destination folder + * @param name a String indicating the name of the file to be created + * @return + * @throws IOException thrown when file creation cannot be performed + */ private static File makeFile(File destination, String name) throws IOException { String[] dirs = name.split("\\\\"); @@ -111,6 +127,11 @@ private static File makeFile(File destination, String name) } } + /** + * Creates a directory within another specified directory + * @param fh + * @param destination the directory where the new directory will be placed + */ private static void createDirectory(FileHeader fh, File destination) { File f = null; if (fh.isDirectory() && fh.isUnicode()) { @@ -126,16 +147,23 @@ private static void createDirectory(FileHeader fh, File destination) { } } + /** + * Creates a directory in an specified destination for every directory within the fileName parameter. + * This is done in order to the uncompressed directory to preserve its original file/directory hierarchy. + * @param destination the directory that will become the root directory containing the newly created directory. + * @param fileName the name of the directory to be created. + */ private static void makeDirectory(File destination, String fileName) { String[] dirs = fileName.split("\\\\"); + String path = ""; + if (dirs == null) { return; } - String path = ""; - for (String dir : dirs) { + + for (String dir: dirs) { path = path + File.separator + dir; new File(destination, path).mkdir(); } - } } \ No newline at end of file diff --git a/src/java/main/br/ufpe/cin/groundhog/extractor/UncompressNotSupportedException.java b/src/java/main/br/ufpe/cin/groundhog/extractor/UncompressNotSupportedException.java index c6c8bf5..3ab03c8 100644 --- a/src/java/main/br/ufpe/cin/groundhog/extractor/UncompressNotSupportedException.java +++ b/src/java/main/br/ufpe/cin/groundhog/extractor/UncompressNotSupportedException.java @@ -2,10 +2,15 @@ import br.ufpe.cin.groundhog.GroundhogException; +/** + * Exception thrown when an extraction operation is attempted to be performed on an unsupported format + * @author fjsj, gustavopinto, rodrigoalvesvieira + * + */ public class UncompressNotSupportedException extends GroundhogException { private static final long serialVersionUID = -5221460516926877262L; public UncompressNotSupportedException(String msg) { super(msg); - } + } } \ No newline at end of file diff --git a/src/java/main/br/ufpe/cin/groundhog/util/FileUtil.java b/src/java/main/br/ufpe/cin/groundhog/util/FileUtil.java index f092e89..28f39bb 100644 --- a/src/java/main/br/ufpe/cin/groundhog/util/FileUtil.java +++ b/src/java/main/br/ufpe/cin/groundhog/util/FileUtil.java @@ -12,7 +12,7 @@ import com.google.common.io.Files; /** - * + * General utilities class for file operations * @author fjsj, gustavopinto, rodrigoalvesvieira */ public class FileUtil { @@ -78,6 +78,6 @@ public void writeStringToFile(File file, String data) throws IOException { */ public void copyDirectory(File srcDir, File destDir) throws IOException { FileUtils.copyDirectory(srcDir, destDir); - // TODO: add tests + // TODO: add tests } } \ No newline at end of file