Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Clean up uncompressArchive function to remove duplication (DOECODE-1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamar2 committed Mar 22, 2023
1 parent 4b9165b commit 745f2dc
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions src/main/java/gov/osti/archiver/util/Extractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,42 @@ public static String uncompressArchive(Project project) throws IOException, Arch
if (null==project.getFileName())
return null;

// ZipInputStream has known issues with extracting some types of
// archives. ZipFile is the reccommended way to handle and isn't
// compatible with the way other archives are handled
if(project.getFileName().toLowerCase().endsWith(".zip"))
return uncompressZipArchive(project);
boolean isLimited = project.getIsLimited();
String targetBaseDir = isLimited ? FILE_LIMITED_BASEDIR : FILE_BASEDIR;

// uncompress the archive into the PROJECT folder
Path base_file_path = Paths.get(
targetBaseDir,
String.valueOf(project.getProjectId()));
targetBaseDir,
String.valueOf(project.getProjectId()));
File folder = base_file_path.toFile();
if (!folder.exists())
throw new IOException ("Extraction folder does not exist.");


// ZipInputStream has known issues with extracting some types of
// archives. ZipFile is the reccommended way to handle and isn't
// compatible with the way other archives are handled
if(project.getFileName().toLowerCase().endsWith(".zip"))
return uncompressZipArchive(project, base_file_path);
else
return uncompressOtherArchive(project, base_file_path);
}

/*
* Given a Project with a FileName attached and not a zip archive,
* attempt to uncompress the archive file into a sub-folder.
*
* Filename is considered to be an absolute path; contents will be
* uncompressed into a folder based on the configured FILE BASEDIR value and
* PROJECT ID. Each PROJECT is considered to be a UNIQUE archive area.
*
* @param project the Project in question
* @param base_file_path the location to save the archive
* @return the filename path to the git repository created, or null if
* none/no file to uncompress
* @throws IOException on file IO errors
* @throws ArchiveException on uncompress extraction errors
*/
private static String uncompressOtherArchive(Project project, Path base_file_path) throws IOException, ArchiveException {
// open the archiver stream
ArchiveInputStream in = openArchiveStream(project.getFileName());
// iterate through the Archive, creating folders and extracting files.
Expand Down Expand Up @@ -184,37 +204,25 @@ public static String uncompressArchive(Project project) throws IOException, Arch

// send back the file path created
return base_file_path.toString();
}
}


/**
* Given a Project with a FileName attached, attempt to uncompress the Zip
* archive file into a sub-folder.
* Given a Project with a FileName attached and is a zip archive,
* attempt to uncompress the archive file into a sub-folder.
*
* Filename is considered to be an absolute path; contents will be
* uncompressed into a folder based on the configured FILE BASEDIR value and
* PROJECT ID. Each PROJECT is considered to be a UNIQUE archive area.
*
* @param project the Project in question
* @param base_file_path the location to save the archive
* @return the filename path to the git repository created, or null if
* none/no file to uncompress
* @throws IOException on file IO errors
* @throws ArchiveException on uncompress extraction errors
*/
public static String uncompressZipArchive(Project project) throws IOException, ArchiveException {
if (null==project.getFileName())
return null;

boolean isLimited = project.getIsLimited();
String targetBaseDir = isLimited ? FILE_LIMITED_BASEDIR : FILE_BASEDIR;

// uncompress the archive into the PROJECT folder
Path base_file_path = Paths.get(
targetBaseDir,
String.valueOf(project.getProjectId()));
File folder = base_file_path.toFile();
if (!folder.exists())
throw new IOException ("Extraction folder does not exist.");

private static String uncompressZipArchive(Project project, Path base_file_path) throws IOException, ArchiveException {
// open the Zip file
try ( ZipFile zipFile = new ZipFile(project.getFileName()) )
{
Expand Down

0 comments on commit 745f2dc

Please sign in to comment.