Skip to content

Commit

Permalink
Fixed spotless issues concerning zips.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Oct 31, 2023
1 parent 7774c0c commit cc6e49f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
* Helper class to perform all necessary operations or functions on files or folders.
*/
public class FileHelper {
private static final int ZIP_THRESHOLD_ENTRIES = 100000;
private static final int ZIP_THRESHOLD_SIZE = 1000000000;
private static final double ZIP_THRESHOLD_RATIO = 10;
private static final String ZIP_BOMB_ERROR_MESSAGE = "Refusing to unzip file (%s), because it seems to be a fork bomb";

private FileHelper() {
// private constructor to prevent instantiation
}

/**
* Returns the name of the passed file, trimming its file extension.
*
* @param file is the file to obtain the name from
* @return returns the name of the file without file extension
*/
Expand All @@ -31,6 +36,7 @@ public static String getFileNameWithoutFileExtension(File file) {

/**
* Creates directory if it dose not exist
*
* @param directory to be created
* @throws IOException if the directory could not be created
*/
Expand All @@ -42,6 +48,7 @@ public static void createDirectoryIfItDoesNotExist(File directory) throws IOExce

/**
* Creates file if it dose not exist
*
* @param file to be created
* @throws IOException if the file could not be created
*/
Expand All @@ -60,23 +67,47 @@ private static String createNewIOExceptionStringForFileOrFOlderCreation(File fil
}

public static void unzip(File zip, File targetDirectory) throws IOException {
ZipFile zipFile = new ZipFile(zip);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
new File(targetDirectory, entry.getName()).mkdirs();
} else {
File outputFile = new File(targetDirectory, entry.getName());
outputFile.getParentFile().mkdirs();
try (ZipFile zipFile = new ZipFile(zip)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();

InputStream inputStream = zipFile.getInputStream(entry);
OutputStream outputStream = new FileOutputStream(outputFile);
inputStream.transferTo(outputStream);
inputStream.close();
outputStream.close();
long totalSizeArchive = 0;
long totalEntriesArchive = 0;

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

ZipEntry entry = entries.nextElement();
File unzippedFile = new File(targetDirectory, entry.getName()).getCanonicalFile();

if (unzippedFile.getAbsolutePath().startsWith(targetDirectory.getAbsolutePath())) {
if (entry.isDirectory()) {
unzippedFile.mkdirs();
} else {
unzippedFile.getParentFile().mkdirs();

InputStream inputStream = zipFile.getInputStream(entry);
OutputStream outputStream = new FileOutputStream(unzippedFile);
long count = inputStream.transferTo(outputStream);
totalSizeArchive += count;
totalSizeEntry += count;
inputStream.close();
outputStream.close();

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

if (totalSizeArchive > ZIP_THRESHOLD_SIZE) {
throw new IllegalStateException(ZIP_BOMB_ERROR_MESSAGE);
}
if (totalEntriesArchive > ZIP_THRESHOLD_ENTRIES) {
throw new IllegalStateException();
}
}
}
zipFile.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ public class UnzipManager {
private final Map<DataSet, File> unzippedFiles;
private static UnzipManager instance;

private static UnzipManager getInstance() {
private synchronized static UnzipManager getInstance() {
if (instance == null) {
synchronized (UnzipManager.class) {
if (instance == null) {
instance = new UnzipManager();
}
}
instance = new UnzipManager();
}

return instance;
Expand Down

0 comments on commit cc6e49f

Please sign in to comment.