Skip to content

Commit

Permalink
Merge branch 'main' of github.com:FormikoLudo/Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
HydrolienF committed May 29, 2024
2 parents 2a17417 + 5f50090 commit 47c7da6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
12 changes: 8 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
jacoco
}

version = "0.0.4"
version = "0.0.7"
group = "fr.formiko.utils"

repositories {
Expand All @@ -16,8 +16,7 @@ repositories {

dependencies {
// Use JUnit Jupiter for testing.
testImplementation(libs.junit.jupiter)

testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

// This dependency is exported to consumers, that is to say found on their compile classpath.
Expand All @@ -32,14 +31,19 @@ dependencies {
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(17)
}
}

tasks.assemble {
dependsOn("jar")
}

// utf 8 encoding
tasks.compileJava {
options.encoding = "UTF-8"
}

tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
Expand Down
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
[versions]
commons-math3 = "3.6.1"
guava = "32.1.3-jre"
junit-jupiter = "5.10.0"

[libraries]
commons-math3 = { module = "org.apache.commons:commons-math3", version.ref = "commons-math3" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
53 changes: 47 additions & 6 deletions src/main/java/fr/formiko/utils/FLUFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -69,6 +70,7 @@ private static void setDownloadingMessage(String message) {
public static boolean appendToFile(String path, String content) { return internal.appendToFile(path, content); }

public static List<String> listFiles(String path) { return internal.listFiles(path); }
public static List<String> listFilesRecursively(String path) { return internal.listFilesRecursively(path); }

public static boolean zip(String source, String destination) { return internal.zip(source, destination); }
public static boolean unzip(String source, String destination, String directoryInsideZipToGet) {
Expand All @@ -83,7 +85,7 @@ public static boolean downloadAndUnzip(String url, String destination, String di
public static boolean downloadAndUnzip(String url, String destination) { return downloadAndUnzip(url, destination, ""); }
public static String downloadAndRead(String url) { return readFileFromWeb(url); }
public static int countEntryOfZipFile(String url) { return internal.countEntryOfZipFile(url); }
public static long getSize(String path) { return -1; }
public static long getSize(String path) { return internal.getSize(path); }

public static boolean setMaxPermission(String path, boolean recursive) { return false; }
public static boolean setMaxPermission(String path) { return setMaxPermission(path, true); }
Expand All @@ -109,9 +111,10 @@ private boolean createFile(String path) {
createParents(file);
// setDownloadingValue(currentAction++ / (double) actionToDo);
// setDownloadingMessage("Creating file");
boolean r = file.createNewFile();
// boolean r = file.createNewFile();
// setDownloadingValue(currentAction++ / (double) actionToDo);
return r;
// return r;
return file.createNewFile();
} catch (IOException e) {
return false;
}
Expand Down Expand Up @@ -254,8 +257,33 @@ private boolean appendToFile(String path, String content) {

private List<String> listFiles(String path) {
if (isAValidePath(path)) {
String[] list = new File(path).list();
return list == null ? null : Arrays.asList(list);
File[] list = new File(path).listFiles();
return list == null ? null : Arrays.stream(list).map(f -> {
if (f.isDirectory()) {
return f.getName() + FILE_SEPARATOR;
} else {
return f.getName();
}
}).toList();
} else {
return null;
}
}

private List<String> listFilesRecursively(String path) {
if (isAValidePath(path)) {
return Arrays.stream(new File(path).listFiles()).map(f -> {
if (f.isDirectory()) {
// return listFilesRecursively(f.getAbsolutePath());
List<String> l = new LinkedList<>();
l.add(f.getName());
// Add all sub files with the same path
listFilesRecursively(f.getAbsolutePath()).forEach(subFile -> l.add(f.getName() + FILE_SEPARATOR + subFile));
return l;
} else {
return List.of(f.getName());
}
}).flatMap(List::stream).toList();
} else {
return null;
}
Expand Down Expand Up @@ -303,7 +331,7 @@ private void zipFile(File fileToZip, String fileName, String destination, ZipOut
}
private void createParents(String path) { createParents(new File(path)); }
private void createParents(File file) {
if (file != null) {
if (file != null && file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
}
Expand Down Expand Up @@ -431,6 +459,19 @@ private int countEntryOfZipFile(String url) {
}
}

private long getSize(String path) {
if (isAValidePath(path)) {
File f = new File(path);
if (f.isDirectory()) {
return Arrays.stream(f.listFiles()).mapToLong(file -> getSize(file.getAbsolutePath())).sum();
} else {
return f.length();
}
} else {
return -1;
}
}


class FLUProgressionThread extends Thread {
private File fileOut;
Expand Down

0 comments on commit 47c7da6

Please sign in to comment.