Skip to content

Commit

Permalink
Refactor FileUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
quanpham-axonivy committed Dec 25, 2024
1 parent a648020 commit d48ba91
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand All @@ -21,9 +22,7 @@ public class FileUtils {
public static File createFile(String fileName) throws IOException {
File file = new File(fileName);
File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Failed to create directory: " + parentDir.getAbsolutePath());
}
createDirectoryFromFile(parentDir);
if (!file.exists() && !file.createNewFile()) {
throw new IOException("Failed to create file: " + file.getAbsolutePath());
}
Expand All @@ -46,22 +45,16 @@ public static void unzip(MultipartFile file, String location) throws IOException
Path entryPath = Paths.get(entry.getName()).normalize();
Path resolvedPath = extractDir.toPath().resolve(entryPath).normalize();

// Ensure the resolved path is within the target directory
if (!resolvedPath.startsWith(extractDir.toPath())) {
throw new IOException("Entry is outside the target dir: " + entry.getName());
}

File outFile = resolvedPath.toFile();
if (entry.isDirectory()) {
if (!outFile.mkdirs() && !outFile.isDirectory()) {
throw new IOException("Failed to create directory: " + outFile);
}
createDirectoryFromFile(outFile);
} else {
File parentDir = outFile.getParentFile();
if (parentDir != null && !parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Failed to create parent directory: " + parentDir);
}

createDirectoryFromFile(parentDir);
try (FileOutputStream fos = new FileOutputStream(outFile)) {
byte[] buffer = new byte[1024];
int length;
Expand All @@ -77,6 +70,11 @@ public static void unzip(MultipartFile file, String location) throws IOException
}
}

private static void createDirectoryFromFile(File file) throws IOException {
if (file != null && !file.mkdirs() && !file.isDirectory()) {
throw new IOException("Failed to create directory: " + file);
}
}

public static void prepareUnZipDirectory(Path directory) throws IOException {
clearDirectory(directory);
Expand All @@ -85,10 +83,11 @@ public static void prepareUnZipDirectory(Path directory) throws IOException {

public static void clearDirectory(Path path) throws IOException {
if (Files.exists(path)) {
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(java.io.File::delete);
try (Stream<Path> paths = Files.walk(path)) {
paths.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

;

@ExtendWith(MockitoExtension.class)
class ReleasePreviewServiceImplTest {

Expand Down

0 comments on commit d48ba91

Please sign in to comment.