Skip to content

Commit 568a23f

Browse files
committed
list files and zip
1 parent 841c4cc commit 568a23f

File tree

4 files changed

+130
-7
lines changed

4 files changed

+130
-7
lines changed

src/main/java/fr/formiko/utils/FLUFiles.java

+54-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import java.nio.file.Files;
88
import java.nio.file.Paths;
99
import java.nio.file.StandardOpenOption;
10+
import java.util.Arrays;
1011
import java.util.List;
12+
import java.util.zip.ZipEntry;
13+
import java.util.zip.ZipOutputStream;
1114

1215
/**
1316
* A utility class to manipulate files.
@@ -37,9 +40,9 @@ private FLUFiles() {} // hide constructor
3740
public static boolean writeFile(String path, String content) { return internal.writeFile(path, content); }
3841
public static boolean appendToFile(String path, String content) { return internal.appendToFile(path, content); }
3942

40-
public static List<String> listFiles(String path) { return null; }
43+
public static List<String> listFiles(String path) { return internal.listFiles(path); }
4144

42-
public static boolean zip(String source, String destination) { return false; }
45+
public static boolean zip(String source, String destination) { return internal.zip(source, destination); }
4346
public static boolean unzip(String source, String destination, String folderIncideZipToGet) { return false; }
4447
public static boolean unzip(String source, String destination) { return unzip(source, destination, "."); }
4548

@@ -66,7 +69,7 @@ private boolean createFile(String path) {
6669
if (isAValidePath(path)) {
6770
try {
6871
File file = new File(path);
69-
file.getParentFile().mkdirs();
72+
createParents(file);
7073
return file.createNewFile();
7174
} catch (IOException e) {
7275
return false;
@@ -103,7 +106,7 @@ private boolean delete(String path) {
103106
private boolean copy(String source, String destination) {
104107
if (isAValidePath(source) && isAValidePath(destination)) {
105108
File destinationFile = new File(destination);
106-
destinationFile.getParentFile().mkdirs();
109+
createParents(destinationFile);
107110
File sourceFile = new File(source);
108111
if (!sourceFile.exists()) {
109112
return false;
@@ -131,7 +134,7 @@ private boolean copy(String source, String destination) {
131134
private boolean move(String source, String destination) {
132135
if (isAValidePath(source) && isAValidePath(destination)) {
133136
File destinationFile = new File(destination);
134-
destinationFile.getParentFile().mkdirs();
137+
createParents(destinationFile);
135138
return new File(source).renameTo(destinationFile);
136139
} else {
137140
return false;
@@ -178,7 +181,7 @@ private String readFileFromWeb(String urlString) {
178181
*/
179182
private boolean writeFile(String path, String content) {
180183
if (isAValidePath(path)) {
181-
new File(path).getParentFile().mkdirs();
184+
createParents(path);
182185
try {
183186
Files.writeString(Paths.get(path), content, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
184187
return true;
@@ -195,7 +198,7 @@ private boolean writeFile(String path, String content) {
195198
*/
196199
private boolean appendToFile(String path, String content) {
197200
if (isAValidePath(path)) {
198-
new File(path).getParentFile().mkdirs();
201+
createParents(path);
199202
try {
200203
Files.writeString(Paths.get(path), content, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
201204
return true;
@@ -206,5 +209,49 @@ private boolean appendToFile(String path, String content) {
206209
return false;
207210
}
208211
}
212+
213+
private List<String> listFiles(String path) {
214+
if (isAValidePath(path)) {
215+
String[] list = new File(path).list();
216+
return list == null ? null : Arrays.asList(list);
217+
} else {
218+
return null;
219+
}
220+
}
221+
222+
private boolean zip(String source, String destination) {
223+
if (isAValidePath(source) && isAValidePath(destination)) {
224+
destination = FLUStrings.addAtTheEndIfNeeded(destination, ".zip");
225+
createParents(destination);
226+
File sourceFile = new File(source);
227+
if (!sourceFile.exists()) {
228+
return false;
229+
}
230+
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(Paths.get(destination)))) {
231+
zipFile(sourceFile, sourceFile.getName(), destination, zos);
232+
return true;
233+
} catch (IOException e) {
234+
return false;
235+
}
236+
} else {
237+
return false;
238+
}
239+
}
240+
private void zipFile(File fileToZip, String fileName, String destination, ZipOutputStream zos) throws IOException {
241+
if (fileToZip.isDirectory()) {
242+
fileName = FLUStrings.addAtTheEndIfNeeded(fileName, File.separator);
243+
zos.putNextEntry(new ZipEntry(fileName));
244+
zos.closeEntry();
245+
for (File file : fileToZip.listFiles()) {
246+
zipFile(file, fileName + file.getName(), destination, zos);
247+
}
248+
} else {
249+
zos.putNextEntry(new ZipEntry(fileName));
250+
Files.copy(fileToZip.toPath(), zos);
251+
zos.closeEntry();
252+
}
253+
}
254+
private void createParents(String path) { createParents(new File(path)); }
255+
private void createParents(File file) { file.getParentFile().mkdirs(); }
209256
}
210257
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package fr.formiko.utils;
2+
3+
public class FLUStrings {
4+
private FLUStrings() {} // hide constructor
5+
public static String addAtTheEndIfNeeded(String string, String toAdd) {
6+
if (string == null) {
7+
string = "";
8+
}
9+
if (toAdd == null) {
10+
toAdd = "";
11+
}
12+
return string.endsWith(toAdd) ? string : string + toAdd;
13+
}
14+
}

src/test/java/fr/formiko/utils/FLUFilesTest.java

+42
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,46 @@ private static Stream<Arguments> testAppendToExistingFileFileSource() {
253253
Arguments.of(TEST_PATH + "existingFile4", true, "Some content", "ABC\nSome content",
254254
TEST_PATH_TEMPORARY + "existingFile4"));
255255
}
256+
257+
@ParameterizedTest
258+
@MethodSource("testListFilesSource")
259+
void testListFiles(String path, boolean shouldWork, List<String> expectedFiles) {
260+
clean();
261+
if (shouldWork) {
262+
List<String> files = FLUFiles.listFiles(path);
263+
assertEquals(expectedFiles.size(), files.size());
264+
for (String s : expectedFiles) {
265+
assertTrue(files.contains(s));
266+
}
267+
} else {
268+
assertNull(FLUFiles.listFiles(path));
269+
}
270+
}
271+
272+
private static Stream<Arguments> testListFilesSource() {
273+
return Stream.of(
274+
Arguments.of(TEST_PATH, true, List.of("existingDir", "existingFile.x", "existingFile2", "existingFile3", "existingFile4")),
275+
Arguments.of(TEST_PATH + "existingDir/", true, List.of("subDir")),
276+
Arguments.of(TEST_PATH + "existingDir/subDir/", true, List.of("existingFile.txt")), Arguments.of(null, false, null),
277+
Arguments.of(TEST_PATH + "unexistingDirectory", false, null));
278+
}
279+
280+
@ParameterizedTest
281+
@MethodSource("testZipSource")
282+
void testZip(String path, boolean shouldWork, String destination, String realDestination) {
283+
assertEquals(shouldWork, FLUFiles.zip(path, destination));
284+
if (shouldWork) {
285+
assertEquals(true, FLUFiles.delete(realDestination));
286+
}
287+
}
288+
289+
private static Stream<Arguments> testZipSource() {
290+
return Stream.of(
291+
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "existingDir.zip",
292+
TEST_PATH_TEMPORARY + "existingDir.zip"),
293+
Arguments.of(TEST_PATH + "existingDir", true, TEST_PATH_TEMPORARY + "existingDirZ2",
294+
TEST_PATH_TEMPORARY + "existingDirZ2.zip"),
295+
Arguments.of(TEST_PATH + "unexistingDirectoryTYUI", false, TEST_PATH_TEMPORARY + "unexistingDirectory.zip", null),
296+
Arguments.of("existingDir", false, null, null), Arguments.of(null, false, null, null));
297+
}
256298
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package fr.formiko.utils;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import java.util.stream.Stream;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
public class FLUStringsTest {
10+
@ParameterizedTest
11+
@MethodSource("testAddAtTheEndIfNeededSource")
12+
public void testAddAtTheEndIfNeeded(String string, String toAdd, String expected) {
13+
assertEquals(expected, FLUStrings.addAtTheEndIfNeeded(string, toAdd));
14+
}
15+
16+
private static Stream<Arguments> testAddAtTheEndIfNeededSource() {
17+
return Stream.of(Arguments.of("test", "test", "test"), Arguments.of("test1", "test", "test1test"),
18+
Arguments.of("OPJ", ".txt", "OPJ.txt"), Arguments.of(null, ".txt", ".txt"), Arguments.of("A", null, "A"));
19+
}
20+
}

0 commit comments

Comments
 (0)