Skip to content

Commit 217e741

Browse files
committed
fix unzip
1 parent 41a478c commit 217e741

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

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

+34-18
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ private FLUFiles() {} // hide constructor
4545
public static List<String> listFiles(String path) { return internal.listFiles(path); }
4646

4747
public static boolean zip(String source, String destination) { return internal.zip(source, destination); }
48-
public static boolean unzip(String source, String destination, String folderIncideZipToGet) {
49-
return internal.unzip(source, destination, folderIncideZipToGet);
48+
public static boolean unzip(String source, String destination, String directoryInsideZipToGet) {
49+
return internal.unzip(source, destination, directoryInsideZipToGet);
5050
}
51-
public static boolean unzip(String source, String destination) { return unzip(source, destination, "."); }
51+
public static boolean unzip(String source, String destination) { return unzip(source, destination, ""); }
5252

5353
public static boolean download(String url, String destination, boolean withProgressInfo) { return false; }
5454
public static boolean download(String url, String destination) { return download(url, destination, false); }
55-
public static boolean downloadAndUnzip(String url, String destination, String folderIncideZipToGet) { return false; }
56-
public static boolean downloadAndUnzip(String url, String destination) { return downloadAndUnzip(url, destination, "."); }
55+
public static boolean downloadAndUnzip(String url, String destination, String directoryInsideZipToGet) { return false; }
56+
public static boolean downloadAndUnzip(String url, String destination) { return downloadAndUnzip(url, destination, ""); }
5757
public static String downloadAndRead(String url) { return null; }
5858
public static int countEntryOfZipFile(String url) { return -1; }
5959
public static long getSize(String path) { return -1; }
@@ -67,7 +67,7 @@ public static boolean unzip(String source, String destination, String folderInci
6767
private static class FLUFilesInternal {
6868
private FLUFilesInternal() {} // hide constructor
6969

70-
private static boolean isAValidePath(String path) { return path != null && !path.isBlank(); }
70+
private boolean isAValidePath(String path) { return path != null; }
7171

7272
private boolean createFile(String path) {
7373
if (isAValidePath(path)) {
@@ -281,35 +281,51 @@ private void zipFile(File fileToZip, String fileName, String destination, ZipOut
281281
}
282282
}
283283
private void createParents(String path) { createParents(new File(path)); }
284-
private void createParents(File file) { file.getParentFile().mkdirs(); }
284+
private void createParents(File file) {
285+
if (file != null) {
286+
file.getParentFile().mkdirs();
287+
}
288+
}
285289

286-
private boolean unzip(String source, String destination, String folderIncideZipToGet) {
290+
private boolean unzip(String source, String destination, String directoryInsideZipToGet) {
287291
if (isAValidePath(source) && isAValidePath(destination)) {
288292
source = FLUStrings.addAtTheEndIfNeeded(source, ".zip");
289293
File destinationFile = new File(destination);
290294
createParents(destinationFile);
291295
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Paths.get(source)))) {
292296
for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
293-
createZipEntry(destination, folderIncideZipToGet, zis, entry);
297+
createZipEntry(destination, directoryInsideZipToGet, zis, entry);
294298
}
295-
return false;
299+
return true;
296300
} catch (Exception e) {
301+
System.out.println(e);
302+
e.printStackTrace();
297303
return false;
298304
}
299305
} else {
300306
return false;
301307
}
302308
}
303-
private void createZipEntry(String destination, String folderIncideZipToGet, ZipInputStream zis, ZipEntry entry)
309+
private void createZipEntry(String destination, String directoryInsideZipToGet, ZipInputStream zis, ZipEntry entry)
304310
throws IOException {
305-
if (entry.getName().startsWith(folderIncideZipToGet)) {
306-
String filePath = destination + File.separator + entry.getName();
307-
if (entry.isDirectory()) {
308-
createDirectory(filePath);
309-
} else {
310-
createParents(filePath);
311-
Files.copy(zis, Paths.get(filePath));
311+
File destinationFile = new File(destination);
312+
String absoluteDestinationPath = FLUStrings.addAtTheEndIfNeeded(destinationFile.getAbsolutePath(), File.separator);
313+
// System.out.println("absoluteDestinationPath: " + absoluteDestinationPath);
314+
String entryName = entry.getName();
315+
// System.out.println("entryName before: " + entryName);
316+
if (entryName.startsWith(directoryInsideZipToGet)) {
317+
entryName = entryName.substring(Math.max(0, directoryInsideZipToGet.length() - 1));
318+
File fileToCreate = new File(destination, entryName);
319+
if (fileToCreate.getAbsolutePath().startsWith(absoluteDestinationPath)) {
320+
// System.out.println("entryName: " + entryName);
321+
if (entry.isDirectory()) {
322+
createDirectory(absoluteDestinationPath + entryName);
323+
} else {
324+
createParents(absoluteDestinationPath + entryName);
325+
Files.copy(zis, Paths.get(absoluteDestinationPath + entryName));
326+
}
312327
}
328+
313329
}
314330
}
315331
}

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

+24-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class FLUFilesTest {
2828

2929
private static Stream<Arguments> testIsAValidePathSource() {
3030
return Stream.of(Arguments.of(".", true), Arguments.of("..", true), Arguments.of("~/", true), Arguments.of("../Utils/", true),
31-
Arguments.of(".gitignore", true), Arguments.of("", false), Arguments.of(null, false));
31+
Arguments.of(".gitignore", true), Arguments.of("", true), Arguments.of(null, false));
3232
}
3333

3434
@ParameterizedTest
@@ -89,7 +89,7 @@ private static Stream<Arguments> testCopySource() {
8989
Arguments.of(TEST_PATH + "unexistingFile.x", false, TEST_PATH_TEMPORARY + "unexistingFile.x"), // copy of missing file
9090
Arguments.of(TEST_PATH_TEMPORARY + "existingFile.x", false, TEST_PATH_TEMPORARY + "existingFile.x"), // don't exist here
9191
Arguments.of(TEST_PATH + "existingFile.x", false, TEST_PATH + "existingFile.x"), // same location
92-
Arguments.of(null, false, TEST_PATH), Arguments.of(TEST_PATH + "existingFile.x", false, ""));
92+
Arguments.of(null, false, TEST_PATH), Arguments.of(TEST_PATH + "existingFile.x", false, null));
9393
}
9494

9595
@ParameterizedTest
@@ -298,16 +298,26 @@ private static Stream<Arguments> testZipSource() {
298298

299299
@ParameterizedTest
300300
@MethodSource("testUnzipSource")
301-
void testUnzip(String pathToZip, boolean shouldWork, String zipedFile, String pathToDownloadIntoZip) {
302-
assertEquals(shouldWork, FLUFiles.zip(pathToZip, zipedFile));
303-
assertEquals(shouldWork, FLUFiles.unzip(zipedFile, pathToDownloadIntoZip));
304-
// TODO
305-
// assertEquals(shouldWork, FLUFiles.unzip(path, destination));
306-
// if (shouldWork) {
307-
// assertTrue(new File(destination + fileToFind).exists());
308-
// assertEquals(true, FLUFiles.delete(destination));
309-
// }
310-
}
311-
312-
private static Stream<Arguments> testUnzipSource() { return Stream.of(); }
301+
void testUnzip(String pathToBeZip, boolean shouldWork, String zipedFile, String pathToDownloadIntoZip, String fileToCheck) {
302+
assertEquals(shouldWork, FLUFiles.zip(pathToBeZip, zipedFile));
303+
assertEquals(shouldWork, FLUFiles.unzip(zipedFile, TEST_PATH_TEMPORARY, pathToDownloadIntoZip));
304+
if (shouldWork) {
305+
assertTrue(new File(fileToCheck).exists());
306+
// assertEquals(true, FLUFiles.delete(zipedFile));
307+
// assertEquals(true, FLUFiles.delete(pathToDownloadIntoZip));
308+
}
309+
}
310+
311+
private static Stream<Arguments> testUnzipSource() {
312+
return Stream.of(
313+
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "createdZip", "",
314+
TEST_PATH_TEMPORARY + "existingDir/"),
315+
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "createdZip", "existingDir/subDir/",
316+
TEST_PATH_TEMPORARY + "existingFile.txt"));
317+
}
318+
319+
public static void main(String[] args) {
320+
new FLUFilesTest().testUnzip(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "createdZip", "",
321+
TEST_PATH_TEMPORARY + "existingDir/");
322+
}
313323
}

0 commit comments

Comments
 (0)