Skip to content

Commit 41a478c

Browse files
committed
unzip
1 parent 568a23f commit 41a478c

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

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

+61-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.io.InputStream;
56
import java.net.URI;
67
import java.net.URL;
78
import java.nio.file.Files;
@@ -10,6 +11,7 @@
1011
import java.util.Arrays;
1112
import java.util.List;
1213
import java.util.zip.ZipEntry;
14+
import java.util.zip.ZipInputStream;
1315
import java.util.zip.ZipOutputStream;
1416

1517
/**
@@ -43,7 +45,9 @@ private FLUFiles() {} // hide constructor
4345
public static List<String> listFiles(String path) { return internal.listFiles(path); }
4446

4547
public static boolean zip(String source, String destination) { return internal.zip(source, destination); }
46-
public static boolean unzip(String source, String destination, String folderIncideZipToGet) { return false; }
48+
public static boolean unzip(String source, String destination, String folderIncideZipToGet) {
49+
return internal.unzip(source, destination, folderIncideZipToGet);
50+
}
4751
public static boolean unzip(String source, String destination) { return unzip(source, destination, "."); }
4852

4953
public static boolean download(String url, String destination, boolean withProgressInfo) { return false; }
@@ -129,6 +133,29 @@ private boolean copy(String source, String destination) {
129133
return false;
130134
}
131135
}
136+
// private boolean copy(File source, OutputStream destination) {
137+
// if (isAValidePath(source.getName())) {
138+
// if (source == null || !source.exists()) {
139+
// return false;
140+
// }
141+
// if (source.isDirectory()) {
142+
// boolean flag = true;
143+
// for (String subPath : source.list()) {
144+
// if (!copy(new File(source, subPath), destination)) {
145+
// flag = false;
146+
// }
147+
// }
148+
// return flag;
149+
// }
150+
// try {
151+
// return Files.copy(source.toPath(), destination) > 0;
152+
// } catch (IOException e) {
153+
// return false;
154+
// }
155+
// } else {
156+
// return false;
157+
// }
158+
// }
132159

133160

134161
private boolean move(String source, String destination) {
@@ -170,7 +197,9 @@ private String readFileFromWeb(String urlString) {
170197
}
171198
try {
172199
URL url = URI.create(urlString).toURL();
173-
return new String(url.openStream().readAllBytes());
200+
try (InputStream is = url.openStream()) {
201+
return new String(is.readAllBytes());
202+
}
174203
} catch (IOException e) {
175204
return null;
176205
}
@@ -253,5 +282,35 @@ private void zipFile(File fileToZip, String fileName, String destination, ZipOut
253282
}
254283
private void createParents(String path) { createParents(new File(path)); }
255284
private void createParents(File file) { file.getParentFile().mkdirs(); }
285+
286+
private boolean unzip(String source, String destination, String folderIncideZipToGet) {
287+
if (isAValidePath(source) && isAValidePath(destination)) {
288+
source = FLUStrings.addAtTheEndIfNeeded(source, ".zip");
289+
File destinationFile = new File(destination);
290+
createParents(destinationFile);
291+
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(Paths.get(source)))) {
292+
for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
293+
createZipEntry(destination, folderIncideZipToGet, zis, entry);
294+
}
295+
return false;
296+
} catch (Exception e) {
297+
return false;
298+
}
299+
} else {
300+
return false;
301+
}
302+
}
303+
private void createZipEntry(String destination, String folderIncideZipToGet, ZipInputStream zis, ZipEntry entry)
304+
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));
312+
}
313+
}
314+
}
256315
}
257316
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,19 @@ private static Stream<Arguments> testZipSource() {
295295
Arguments.of(TEST_PATH + "unexistingDirectoryTYUI", false, TEST_PATH_TEMPORARY + "unexistingDirectory.zip", null),
296296
Arguments.of("existingDir", false, null, null), Arguments.of(null, false, null, null));
297297
}
298+
299+
@ParameterizedTest
300+
@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(); }
298313
}

0 commit comments

Comments
 (0)