Skip to content

Commit

Permalink
Fixed "extractIPIfInZipFormat" method to return source when source is…
Browse files Browse the repository at this point in the history
… a directory
  • Loading branch information
johannesk authored and hmiguim committed Jan 15, 2025
1 parent 34bb2c1 commit 95a6ec8
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/main/java/org/roda_project/commons_ip2/utils/ZIPUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,29 @@ private ZIPUtils() {
* file extension (e.g. .zip)
*/
public static Path extractIPIfInZipFormat(final Path source, Path destinationDirectory) throws ParseException {
if (Files.isDirectory(source)) {
return source;
}

Path ipFolderPath = destinationDirectory;
if (!Files.isDirectory(source)) {
try {
ZIPUtils.unzip(source, destinationDirectory);

// 20161111 hsilva: see if the IP extracted has a folder which contains
// the content of the IP (for being compliant with previous way of
// creating SIP in ZIP format, this test/adjustment is needed)
if (Files.exists(destinationDirectory) && !Files.exists(destinationDirectory.resolve(IPConstants.METS_FILE))) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(destinationDirectory)) {
for (Path path : directoryStream) {
if (Files.isDirectory(path) && Files.exists(path.resolve(IPConstants.METS_FILE))) {
ipFolderPath = path;
break;
}
try {
ZIPUtils.unzip(source, destinationDirectory);

// 20161111 hsilva: see if the IP extracted has a folder which contains
// the content of the IP (for being compliant with previous way of
// creating SIP in ZIP format, this test/adjustment is needed)
if (Files.exists(destinationDirectory) && !Files.exists(destinationDirectory.resolve(IPConstants.METS_FILE))) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(destinationDirectory)) {
for (Path path : directoryStream) {
if (Files.isDirectory(path) && Files.exists(path.resolve(IPConstants.METS_FILE))) {
ipFolderPath = path;
break;
}
}
}
} catch (IOException e) {
throw new ParseException("Error unzipping file", e);
}
} catch (IOException e) {
throw new ParseException("Error unzipping file", e);
}

return ipFolderPath;
Expand Down

0 comments on commit 95a6ec8

Please sign in to comment.