Skip to content

Commit 4e2d4e3

Browse files
committed
fix createZipEntry
1 parent e8369af commit 4e2d4e3

File tree

4 files changed

+121
-12
lines changed

4 files changed

+121
-12
lines changed

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

+19-7
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,13 @@ private boolean unzip(InputStream source, String destination, String directoryIn
337337
File destinationFile = new File(destination);
338338
createParents(destinationFile);
339339
try (ZipInputStream zis = new ZipInputStream(source)) {
340+
boolean allOk = true;
340341
for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
341-
createZipEntry(destination, directoryInsideZipToGet, zis, entry);
342+
if (!createZipEntry(destination, directoryInsideZipToGet, zis, entry)) {
343+
allOk = false;
344+
}
342345
}
343-
return true;
346+
return allOk;
344347
} catch (Exception e) {
345348
System.out.println(e);
346349
e.printStackTrace();
@@ -350,26 +353,35 @@ private boolean unzip(InputStream source, String destination, String directoryIn
350353
return false;
351354
}
352355
}
353-
private void createZipEntry(String destination, String directoryInsideZipToGet, ZipInputStream zis, ZipEntry entry)
356+
private boolean createZipEntry(String destination, String directoryInsideZipToGet, ZipInputStream zis, ZipEntry entry)
354357
throws IOException {
358+
directoryInsideZipToGet = FLUStrings.removeAtTheEndIfNeeded(directoryInsideZipToGet.replace('\\', '/'), FILE_SEPARATOR);
355359
File destinationFile = new File(destination);
356360
String absoluteDestinationPath = FLUStrings.addAtTheEndIfNeeded(destinationFile.getAbsolutePath().replace('\\', '/'),
357361
FILE_SEPARATOR);
358-
String entryName = entry.getName();
362+
String entryName = entry.getName().replace('\\', '/');
359363
if (directoryInsideZipToGet.isEmpty() || directoryInsideZipToGet.equals(".") || entryName.startsWith(directoryInsideZipToGet)) {
360-
entryName = entryName.substring(Math.max(0, directoryInsideZipToGet.length() - 1));
364+
// Remove part of the path that we don't want
365+
int charToCut = directoryInsideZipToGet.lastIndexOf('/');
366+
entryName = FLUStrings.removeAtTheBeginningIfNeeded(entryName.substring(Math.max(0, charToCut)), FILE_SEPARATOR);
367+
368+
if (entryName.isEmpty()) {
369+
return true;
370+
}
361371
File fileToCreate = new File(destination, entryName);
362372
if (fileToCreate.getAbsolutePath().replace('\\', '/').startsWith(absoluteDestinationPath)) {
363373
if (entry.isDirectory()) {
364374
if (!createDirectory(absoluteDestinationPath + entryName)) {
365-
System.out.println("Error while creating directory: " + absoluteDestinationPath + entryName);
375+
return false;
366376
}
377+
return true;
367378
} else {
368379
createParents(absoluteDestinationPath + entryName);
369-
Files.copy(zis, Paths.get(absoluteDestinationPath + entryName));
380+
return Files.copy(zis, Paths.get(absoluteDestinationPath + entryName)) >= 0L;
370381
}
371382
}
372383
}
384+
return true;
373385
}
374386

375387
private boolean download(String url, String destination) {

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

+30
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,34 @@ public static String addAtTheEndIfNeeded(String string, String toAdd) {
1111
}
1212
return string.endsWith(toAdd) ? string : string + toAdd;
1313
}
14+
15+
public static String removeAtTheEndIfNeeded(String string, String toRemove) {
16+
if (string == null) {
17+
string = "";
18+
}
19+
if (toRemove == null) {
20+
toRemove = "";
21+
}
22+
return string.endsWith(toRemove) ? string.substring(0, string.length() - toRemove.length()) : string;
23+
}
24+
25+
public static String addAtTheBeginningIfNeeded(String string, String toAdd) {
26+
if (string == null) {
27+
string = "";
28+
}
29+
if (toAdd == null) {
30+
toAdd = "";
31+
}
32+
return string.startsWith(toAdd) ? string : toAdd + string;
33+
}
34+
35+
public static String removeAtTheBeginningIfNeeded(String string, String toRemove) {
36+
if (string == null) {
37+
string = "";
38+
}
39+
if (toRemove == null) {
40+
toRemove = "";
41+
}
42+
return string.startsWith(toRemove) ? string.substring(toRemove.length()) : string;
43+
}
1444
}

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

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package fr.formiko.utils;
22

3-
import fr.formiko.utils.progressions.FLUProgressionCLI;
43
import static org.junit.jupiter.api.Assertions.assertEquals;
54
import static org.junit.jupiter.api.Assertions.assertNull;
65
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -333,8 +332,43 @@ private static Stream<Arguments> testDownloadSource() {
333332
Arguments.of("https://unexisting.url", false, TEST_PATH_TEMPORARY + "unexisting.url", null));
334333
}
335334

335+
@ParameterizedTest
336+
@MethodSource("testDownloadAndUnzipSource")
337+
void testDownloadAndUnzip(String url, boolean shouldWork, String destination, String fileToCheck, String directoryInsideZipToGet) {
338+
assertEquals(shouldWork, FLUFiles.downloadAndUnzip(url, destination, directoryInsideZipToGet));
339+
if (shouldWork) {
340+
assertTrue(new File(fileToCheck).exists());
341+
assertEquals(true, FLUFiles.delete(destination));
342+
}
343+
}
344+
345+
private static Stream<Arguments> testDownloadAndUnzipSource() {
346+
return Stream.of(
347+
Arguments.of("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip", true,
348+
TEST_PATH_TEMPORARY + "kl1/", TEST_PATH_TEMPORARY + "kl1/" + "Kokcinelo3.0.20/", "Kokcinelo3.0.20/"),
349+
Arguments.of("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip", true,
350+
TEST_PATH_TEMPORARY + "kl2/", TEST_PATH_TEMPORARY + "kl2/" + "", "Kokcinelo3.0.20/"),
351+
Arguments.of("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip", true,
352+
TEST_PATH_TEMPORARY + "kl3/", TEST_PATH_TEMPORARY + "kl3/" + "Kokcinelo3.0.20", "Kokcinelo3.0.20/"),
353+
Arguments.of("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip", true,
354+
TEST_PATH_TEMPORARY + "kl4/", TEST_PATH_TEMPORARY + "kl4/" + "icon.png", "Kokcinelo3.0.20/icon.png"),
355+
Arguments.of("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip", true,
356+
TEST_PATH_TEMPORARY + "kl5/", TEST_PATH_TEMPORARY + "kl5/" + "icon.ico", "Kokcinelo3.0.20/icon.ico"),
357+
Arguments.of("https://github.com/HydrolienF/Formiko/releases/download/2.29.23/Formiko2.29.23Linux.zip", true,
358+
TEST_PATH_TEMPORARY + "kl6/", TEST_PATH_TEMPORARY + "kl6/" + "java/", "Formiko2.29.23Linux/java/"),
359+
Arguments.of("https://github.com/HydrolienF/Formiko/releases/download/2.29.23/Formiko2.29.23Linux.zip", true,
360+
TEST_PATH_TEMPORARY + "kl6/", TEST_PATH_TEMPORARY + "kl6/" + "java", "Formiko2.29.23Linux/java"),
361+
Arguments.of("https://unexisting.url", false, TEST_PATH_TEMPORARY + "unexisting.url", null, ""));
362+
}
363+
336364
public static void main(String[] args) {
337-
FLUFiles.setProgression(new FLUProgressionCLI());
338-
FLUFiles.createFile(TEST_PATH_TEMPORARY + "/testCreateFiles1.txt");
365+
// FLUFiles.setProgression(new FLUProgressionCLI());
366+
// FLUFiles.createFile(TEST_PATH_TEMPORARY + "/testCreateFiles1.txt");
367+
// Arguments.of("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip", true,
368+
// TEST_PATH_TEMPORARY + "kl1/", TEST_PATH_TEMPORARY + "kl1/" + "KokcineloLauncher/", "")
369+
clean();
370+
System.out
371+
.println(FLUFiles.downloadAndUnzip("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip",
372+
TEST_PATH_TEMPORARY, "Kokcinelo3.0.20/icon.png"));
339373
}
340374
}

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,48 @@
66
import org.junit.jupiter.params.provider.Arguments;
77
import org.junit.jupiter.params.provider.MethodSource;
88

9-
public class FLUStringsTest {
9+
class FLUStringsTest {
1010
@ParameterizedTest
1111
@MethodSource("testAddAtTheEndIfNeededSource")
12-
public void testAddAtTheEndIfNeeded(String string, String toAdd, String expected) {
12+
void testAddAtTheEndIfNeeded(String string, String toAdd, String expected) {
1313
assertEquals(expected, FLUStrings.addAtTheEndIfNeeded(string, toAdd));
1414
}
1515

1616
private static Stream<Arguments> testAddAtTheEndIfNeededSource() {
1717
return Stream.of(Arguments.of("test", "test", "test"), Arguments.of("test1", "test", "test1test"),
1818
Arguments.of("OPJ", ".txt", "OPJ.txt"), Arguments.of(null, ".txt", ".txt"), Arguments.of("A", null, "A"));
1919
}
20+
21+
@ParameterizedTest
22+
@MethodSource("testRemoveAtTheEndIfNeededSource")
23+
void testRemoveAtTheEndIfNeeded(String string, String toRemove, String expected) {
24+
assertEquals(expected, FLUStrings.removeAtTheEndIfNeeded(string, toRemove));
25+
}
26+
27+
private static Stream<Arguments> testRemoveAtTheEndIfNeededSource() {
28+
return Stream.of(Arguments.of("test", "test", ""), Arguments.of("test1", "test", "test1"), Arguments.of("OPJ.txt", ".txt", "OPJ"),
29+
Arguments.of(null, ".txt", ""), Arguments.of("A", null, "A"));
30+
}
31+
32+
@ParameterizedTest
33+
@MethodSource("testAddAtTheBeginningIfNeededSource")
34+
void testAddAtTheBeginningIfNeeded(String string, String toAdd, String expected) {
35+
assertEquals(expected, FLUStrings.addAtTheBeginningIfNeeded(string, toAdd));
36+
}
37+
38+
private static Stream<Arguments> testAddAtTheBeginningIfNeededSource() {
39+
return Stream.of(Arguments.of("test", "test", "test"), Arguments.of("1test", "test", "test1test"),
40+
Arguments.of("OPJ", "C:", "C:OPJ"), Arguments.of(null, "C:", "C:"), Arguments.of("A", null, "A"));
41+
}
42+
43+
@ParameterizedTest
44+
@MethodSource("testRemoveAtTheBeginningIfNeededSource")
45+
void testRemoveAtTheBeginningIfNeeded(String string, String toRemove, String expected) {
46+
assertEquals(expected, FLUStrings.removeAtTheBeginningIfNeeded(string, toRemove));
47+
}
48+
49+
private static Stream<Arguments> testRemoveAtTheBeginningIfNeededSource() {
50+
return Stream.of(Arguments.of("test", "test", ""), Arguments.of("test1", "test", "1"), Arguments.of("C:OPJ", "C:", "OPJ"),
51+
Arguments.of(null, "C:", ""), Arguments.of("A", null, "A"));
52+
}
2053
}

0 commit comments

Comments
 (0)