Skip to content

Commit dbc8262

Browse files
committed
countEntryOfZipFile
1 parent 4e2d4e3 commit dbc8262

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static boolean downloadAndUnzip(String url, String destination, String di
7777
}
7878
public static boolean downloadAndUnzip(String url, String destination) { return downloadAndUnzip(url, destination, ""); }
7979
public static String downloadAndRead(String url) { return readFileFromWeb(url); }
80-
public static int countEntryOfZipFile(String url) { return -1; }
80+
public static int countEntryOfZipFile(String url) { return internal.countEntryOfZipFile(url); }
8181
public static long getSize(String path) { return -1; }
8282

8383
public static boolean setMaxPermission(String path, boolean recursive) { return false; }
@@ -414,6 +414,22 @@ private boolean downloadAndUnzip(String url, String destination, String director
414414
}
415415
}
416416

417+
private int countEntryOfZipFile(String url) {
418+
if (isAValidePath(url)) {
419+
try (ZipInputStream zis = new ZipInputStream(URI.create(url).toURL().openStream())) {
420+
int count = 0;
421+
while (zis.getNextEntry() != null) {
422+
count++;
423+
}
424+
return count;
425+
} catch (IOException e) {
426+
return -1;
427+
}
428+
} else {
429+
return -1;
430+
}
431+
}
432+
417433

418434
class FLUProgressionThread extends Thread {
419435
private File fileOut;

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

+11
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ private static Stream<Arguments> testDownloadAndUnzipSource() {
361361
Arguments.of("https://unexisting.url", false, TEST_PATH_TEMPORARY + "unexisting.url", null, ""));
362362
}
363363

364+
@ParameterizedTest
365+
@MethodSource("testCountEntryOfZipFileSource")
366+
void testCountEntryOfZipFile(String url, int expectedCount) { assertEquals(expectedCount, FLUFiles.countEntryOfZipFile(url)); }
367+
368+
private static Stream<Arguments> testCountEntryOfZipFileSource() {
369+
return Stream.of(Arguments.of("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip", 9),
370+
Arguments.of("https://github.com/HydrolienF/Formiko/releases/download/2.29.23/Formiko2.29.23Linux.zip", 9),
371+
Arguments.of("https://unexisting.url", -1));
372+
}
373+
374+
364375
public static void main(String[] args) {
365376
// FLUFiles.setProgression(new FLUProgressionCLI());
366377
// FLUFiles.createFile(TEST_PATH_TEMPORARY + "/testCreateFiles1.txt");

0 commit comments

Comments
 (0)