Skip to content

Commit

Permalink
Fixed sonarcloud issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Nov 18, 2023
1 parent 4667032 commit 0c41141
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ private static String createNewIOExceptionStringForFileOrFOlderCreation(File fil
return "The file/folder at the location [" + file.toString() + "] could not be created!";
}

/**
* Unzips a given zip file into a given directory.
* @param zip The zip file to extract
* @param targetDirectory The target directory
* @throws IOException If io operations go wrong
*/
public static void unzip(File zip, File targetDirectory) throws IOException {
try (ZipFile zipFile = new ZipFile(zip)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
Expand All @@ -72,7 +78,6 @@ public static void unzip(File zip, File targetDirectory) throws IOException {

while (entries.hasMoreElements()) {
totalEntriesArchive++;
long totalSizeEntry = 0;

ZipEntry entry = entries.nextElement();
File unzippedFile = new File(targetDirectory, entry.getName()).getCanonicalFile();
Expand All @@ -82,24 +87,7 @@ public static void unzip(File zip, File targetDirectory) throws IOException {
unzippedFile.mkdirs();
} else {
unzippedFile.getParentFile().mkdirs();

try (InputStream inputStream = zipFile.getInputStream(entry)) {
try (OutputStream outputStream = new FileOutputStream(unzippedFile)) {
byte[] buffer = new byte[2048];
int count;
while ((count = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);

totalSizeArchive += count;
totalSizeEntry += count;

double compressionRate = (double) totalSizeEntry / entry.getCompressedSize();
if (compressionRate > ZIP_THRESHOLD_RATIO) {
throw new IllegalStateException(String.format(ZIP_BOMB_ERROR_MESSAGE, zip.getAbsolutePath()));
}
}
}
}
totalSizeArchive += extractZipElement(entry, zipFile, zip, unzippedFile);
}
}

Expand All @@ -112,4 +100,27 @@ public static void unzip(File zip, File targetDirectory) throws IOException {
}
}
}
}

private static long extractZipElement(ZipEntry entry, ZipFile zipFile, File zip, File target) throws IOException {
long totalSizeEntry = 0;

try (InputStream inputStream = zipFile.getInputStream(entry)) {
try (OutputStream outputStream = new FileOutputStream(target)) {
byte[] buffer = new byte[2048];
int count;
while ((count = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);

totalSizeEntry += count;

double compressionRate = (double) totalSizeEntry / entry.getCompressedSize();
if (compressionRate > ZIP_THRESHOLD_RATIO) {
throw new IllegalStateException(String.format(ZIP_BOMB_ERROR_MESSAGE, zip.getAbsolutePath()));
}
}
}
}

return totalSizeEntry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class UnzipManager {
private static UnzipManager instance;
private final Logger logger = Logger.getLogger("Unzip Manager");

private synchronized static UnzipManager getInstance() {
private static synchronized UnzipManager getInstance() {
if (instance == null) {
instance = new UnzipManager();
}
Expand Down
25 changes: 12 additions & 13 deletions endtoend-testing/src/main/java/de/jplag/endtoend/model/DataSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,20 @@ public Set<File> getSourceDirectories() throws IOException {
* @return The source directory
*/
File actualSourceDirectory() throws IOException {
switch (storageFormat == null ? StorageFormat.DIRECTORY : storageFormat) {
case DIRECTORY -> {
String location = sourceLocation;
if (location == null) {
location = String.format(DEFAULT_SOURCE_DIRECTORY, this.name);
}
return new File(TestDirectoryConstants.BASE_PATH_TO_RESOURCES.toFile(), location);
StorageFormat actualStorageFormat = storageFormat == null ? StorageFormat.DIRECTORY : storageFormat;

if (actualStorageFormat == StorageFormat.DIRECTORY) {
String location = sourceLocation;
if (location == null) {
location = String.format(DEFAULT_SOURCE_DIRECTORY, this.name);
}
case ZIP -> {
String location = sourceLocation;
if (location == null) {
location = String.format(DEFAULT_SOURCE_ZIP, this.name);
}
return UnzipManager.unzipOrCache(this, new File(TestDirectoryConstants.BASE_PATH_TO_RESOURCES.toFile(), location));
return new File(TestDirectoryConstants.BASE_PATH_TO_RESOURCES.toFile(), location);
} else if (actualStorageFormat == StorageFormat.ZIP) {
String location = sourceLocation;
if (location == null) {
location = String.format(DEFAULT_SOURCE_ZIP, this.name);
}
return UnzipManager.unzipOrCache(this, new File(TestDirectoryConstants.BASE_PATH_TO_RESOURCES.toFile(), location));
}

throw new IllegalStateException();
Expand Down

0 comments on commit 0c41141

Please sign in to comment.