Skip to content

Commit 1ae1065

Browse files
committed
Fix a test
1 parent 53463d6 commit 1ae1065

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* When a function name does not contains file or directory, it can be used with both.
2424
* All functions return true if it work and false if it doesn't.
2525
* Long opperation that are multithreaded:
26-
* - zip
2726
* - unzip
2827
* - copy
2928
*
@@ -73,6 +72,9 @@ private static void setDownloadingMessage(String message) {
7372
public static List<String> listFilesRecursively(String path) { return internal.listFilesRecursively(path); }
7473

7574
public static boolean zip(String source, String destination) { return internal.zip(source, destination); }
75+
/**
76+
* @param directoryInsideZipToGet If it's a/b/ then b/ will be created. To have only b content, use a/b/*
77+
*/
7678
public static boolean unzip(String source, String destination, String directoryInsideZipToGet) {
7779
return internal.unzip(source, destination, directoryInsideZipToGet);
7880
}
@@ -313,7 +315,7 @@ private void zipFile(File fileToZip, String fileName, String destination, ZipOut
313315
zos.putNextEntry(new ZipEntry(finalfileName));
314316
zos.closeEntry();
315317
List<Boolean> allOk = Arrays.asList(true);
316-
Arrays.stream(fileToZip.listFiles()).parallel().forEach(file -> {
318+
Arrays.stream(fileToZip.listFiles()).forEach(file -> {
317319
try {
318320
zipFile(file, finalfileName + file.getName(), destination, zos);
319321
} catch (IOException e) {
@@ -384,14 +386,18 @@ private boolean unzipAllEntries(String source, String destination, String direct
384386

385387
private boolean createZipEntry(String destination, String directoryInsideZipToGet, InputStream zis, ZipEntry entry)
386388
throws IOException {
389+
boolean noDirectory = directoryInsideZipToGet.endsWith("*");
390+
if (noDirectory) {
391+
directoryInsideZipToGet = directoryInsideZipToGet.substring(0, directoryInsideZipToGet.length() - 1);
392+
}
387393
directoryInsideZipToGet = FLUStrings.removeAtTheEndIfNeeded(directoryInsideZipToGet.replace('\\', '/'), FILE_SEPARATOR);
388394
File destinationFile = new File(destination);
389395
String absoluteDestinationPath = FLUStrings.addAtTheEndIfNeeded(destinationFile.getAbsolutePath().replace('\\', '/'),
390396
FILE_SEPARATOR);
391397
String entryName = entry.getName().replace('\\', '/');
392398
if (directoryInsideZipToGet.isEmpty() || directoryInsideZipToGet.equals(".") || entryName.startsWith(directoryInsideZipToGet)) {
393399
// Remove part of the path that we don't want
394-
int charToCut = directoryInsideZipToGet.lastIndexOf('/');
400+
int charToCut = noDirectory ? directoryInsideZipToGet.length() : directoryInsideZipToGet.lastIndexOf('/');
395401
entryName = FLUStrings.removeAtTheBeginningIfNeeded(entryName.substring(Math.max(0, charToCut)), FILE_SEPARATOR);
396402

397403
if (entryName.isEmpty()) {

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,18 @@ void testUnzip(String pathToBeZip, boolean shouldWork, String zipedFile, String
305305
// assertEquals(true, FLUFiles.delete(zipedFile));
306306
// assertEquals(true, FLUFiles.delete(pathToDownloadIntoZip));
307307
}
308+
clean();
308309
}
309310

310311
private static Stream<Arguments> testUnzipSource() {
311312
return Stream.of(
312313
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "createdZip", "",
313314
TEST_PATH_TEMPORARY + "existingDir/"),
314315
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "createdZip", "existingDir/subDir/",
316+
TEST_PATH_TEMPORARY + "subDir/existingFile.txt"),
317+
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "createdZip", "existingDir/subDir/existingFile.txt",
318+
TEST_PATH_TEMPORARY + "existingFile.txt"),
319+
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "createdZip", "existingDir/subDir/*",
315320
TEST_PATH_TEMPORARY + "existingFile.txt"));
316321
}
317322

@@ -377,9 +382,13 @@ public static void main(String[] args) {
377382
// FLUFiles.createFile(TEST_PATH_TEMPORARY + "/testCreateFiles1.txt");
378383
// Arguments.of("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip", true,
379384
// TEST_PATH_TEMPORARY + "kl1/", TEST_PATH_TEMPORARY + "kl1/" + "KokcineloLauncher/", "")
385+
// clean();
386+
// System.out
387+
// .println(FLUFiles.downloadAndUnzip("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip",
388+
// TEST_PATH_TEMPORARY, "Kokcinelo3.0.20/icon.png"));
389+
380390
clean();
381-
System.out
382-
.println(FLUFiles.downloadAndUnzip("https://github.com/HydrolienF/Kokcinelo/releases/download/3.0.20/KokcineloLauncher.zip",
383-
TEST_PATH_TEMPORARY, "Kokcinelo3.0.20/icon.png"));
391+
// FLUFiles.zip("../teavm/", "teavm.zip");
392+
FLUFiles.unzip("teavm.zip", TEST_PATH_TEMPORARY, "teavm/jso/*");
384393
}
385394
}

0 commit comments

Comments
 (0)